zoukankan      html  css  js  c++  java
  • Gym

    Input
    4
    5
    1 2 1 2 1
    3
    30 20 10
    5
    1 2 2 3 4
    9
    3 1 4 1 5 9 2 6 5
    
    Output
    1
    1
    4
    5

    题意:输入一个n表示天数,下一行跟着长度为n的数组a表示第i天的题目难度a[i]。我们想找到aj-ai=ak-aj成立的式子。

    做法:使用unordered_map(会比map快)记录数字出现的次数,然后找2*a[i]-a[j]出现的次数。

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<set>
    #include<cmath>
    #include<string>
    #include<unordered_map>
    #include<vector>
    #include<ctime>
    #include<stack>
    using namespace std;
    #define mm(a,b) memset(a,b,sizeof(a))
    #define ll long long
    #define ull unsigned long long
    #define mm(a,b) memset(a,b,sizeof(a))
    const int maxn=2e3+10;
    const ll mod=1e9+7;
    unordered_map<int,int> m;
    int a[maxn];
    int t,n;
    ll sum;
    int main()
    {
        scanf("%d",&t);
        while(t--){
            sum=0;
            scanf("%d",&n);
            m.clear();
            for(int i=0;i<n;i++){
                scanf("%d",&a[i]);
                m[a[i]]++;
            }
            for(int i=0;i<n;i++){
                m[a[i]]--;
                for(int j=0;j<i;j++){
                    if(m.count(2*a[i]-a[j])){
                        sum+=m[2*a[i]-a[j]];
                    }
                }
            }
            printf("%lld
    ",sum);
        }
        return 0;
    }
  • 相关阅读:
    每日一题_191208
    每日一题_191207
    每日一题_191206
    每日一题_191205
    每日一题_191204
    每日一题_191203
    每日一题_191202
    每日一题_191201
    每日一题_191130
    2020届成都一诊理科16题
  • 原文地址:https://www.cnblogs.com/Tangent-1231/p/12416168.html
Copyright © 2011-2022 走看看