Codeforces 567C Geometric Progression

题目链接:

http://codeforces.com/problemset/problem/567/C

解题思路:

Let's solve this problem for fixed middle element of progression. This means that if we fix element ai then the progression must consist ofai / k and ai·k elements. It could not be possible, for example, if ai is not divisible by k ().

For fixed middle element one could find the number of sequences by counting how many ai / k elements are placed left from fixed element and how many ai·k are placed right from it, and then multiplying this numbers. To do this, one could use two associative arrays Al and Ar, where for each key x will be stored count of occurences of x placed left (or right respectively) from current element. This could be done with map structure.

Sum of values calculated as described above will give the answer to the problem.

AC代码:

#include <iostream>
#include <cstdio>
#include <map>
using namespace std;

typedef long long ll;
map<ll,ll> L,R,num;
ll a[200005];


int main(){
    int n,k;
    while(~scanf("%d%d",&n,&k)){
        ll sum = 0;
        L.clear();R.clear();
        for(int i = 0; i < n; i++){
            scanf("%lld",&a[i]);
            R[a[i]]++;
        }
        for(int i = 0; i < n; i++){
            R[a[i]]--;
            if(a[i] % k == 0)
                sum += L[a[i]/k]*R[a[i]*k];
            L[a[i]]++;
        }
        printf("%lld\n",sum);
    }
    return 0;
}


你可能感兴趣的:(map)