Codeforces Round #261 (Div. 2) D
D. Pashmak and Parmida's problem
time limit per test
3 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputParmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a1, a2, ..., an. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r andak = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, ai) > f(j, n, aj).
Help Pashmak with the test.
Input
The first line of the input contains an integer n (1 ≤ n ≤ 106). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
Print a single integer — the answer to the problem.
sl :很傻的数据结构题,直接求下逆序数就好了。
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
const int MAX= 1e6+10;
int a[MAX],C[MAX];
map<int,int> hash1,hash2,hash3,cnt1,cnt2;
int lowbit(int x) {
return x&-x;
}
void add(int x,int val) {
for(int i=x;i<MAX;i+=lowbit(i)) {
C[i]+=val;
}
}
int sum(int x) {
int res=0;
for(int i=x;i>0;i-=lowbit(i)) res+=C[i];
return res;
}
int main() {
int n;
while(scanf("%d",&n)==1) {
memset(C,0,sizeof(C));
for(int i=1;i<=n;i++) {
scanf("%d",&a[i]);
cnt1[a[i]]++;
hash1[i]=cnt1[a[i]];
}
long long ans=0;
for(int i=n;i>=1;i--) {
cnt2[a[i]]++;
hash2[i]=cnt2[a[i]];
add(hash2[i],1);
if(i-1>=1)
ans+=sum(hash1[i-1]-1);
}
printf("%I64d\n",ans);
}
return 0;
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
const int MAX= 1e6+10;
int a[MAX],C[MAX];
map<int,int> hash1,hash2,hash3,cnt1,cnt2;
int lowbit(int x) {
return x&-x;
}
void add(int x,int val) {
for(int i=x;i<MAX;i+=lowbit(i)) {
C[i]+=val;
}
}
int sum(int x) {
int res=0;
for(int i=x;i>0;i-=lowbit(i)) res+=C[i];
return res;
}
int main() {
int n;
while(scanf("%d",&n)==1) {
memset(C,0,sizeof(C));
for(int i=1;i<=n;i++) {
scanf("%d",&a[i]);
cnt1[a[i]]++;
hash1[i]=cnt1[a[i]];
}
long long ans=0;
for(int i=n;i>=1;i--) {
cnt2[a[i]]++;
hash2[i]=cnt2[a[i]];
add(hash2[i],1);
if(i-1>=1)
ans+=sum(hash1[i-1]-1);
}
printf("%I64d\n",ans);
}
return 0;
}