【CODEFORCES】 D. Pashmak and Parmida's problem

来源:互联网 发布:docker源码分析 下载 编辑:程序博客网 时间:2024/05/15 00:12

D. Pashmak and Parmida's problem
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Parmida 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 and ak = 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.

Sample test(s)
input
71 2 1 1 2 2 1
output
8
input
31 1 1
output
1
input
51 2 3 4 5
output
0




题解:这一题说实话真心吊......

首先看到题目中的表达式,要求满足那个表达式的 i 和 j ,可以看出题目所要求的是一对逆序对,但是是在两个不同的序列中求逆序对(大概可以表达成 A[I]>B[J]     I<J)。看到数据到一百万,冒泡求肯定不划算,所以想到用线段树。记得先离散化一下,然后在线段树上处理。处理方法如下:


记NUML[I]为从1到 I A[I]出现的个数,NUMR[I]同理(从I 到N),处理的时候和处理单个序列中逆序对 的方法很像,但是要注意下顺序而且 I<>J ,从左到右是先把NUML[I-1]更新,再统计个数。I从1到N。

话说我才知道用scanf 比cin 快- =




#include<iostream>#include<cstring>#include<cstdio>#include<map>using namespace std;struct mine{    int l,r,node;};mine tree[4000002];map<int,int> m1;void build(int k,int l,int r){    int mid;    tree[k].l=l; tree[k].r=r; tree[k].node=0;    if (l==r) return;    mid=(l+r)/2;    build(k*2,l,mid);    build(k*2+1,mid+1,r);}void _insert(int k,int l,int r,int p){    if (tree[k].r==r && tree[k].l==l)    {        tree[k].node+=p;        return;    }    int mid=(tree[k].l+tree[k].r)/2;    if (r<=mid) _insert(k*2,l,r,p);    else if (l>=mid+1) _insert(k*2+1,l,r,p);    else    {        _insert(k*2,l,mid,p);        _insert(k*2+1,mid+1,r,p);    }    tree[k].node=tree[k*2].node+tree[k*2+1].node;}int _count(int k,int l,int r){    if (tree[k].l==l && tree[k].r==r) return tree[k].node;    int mid=(tree[k].l+tree[k].r)/2;    if (r<=mid) return _count(k*2,l,r);    else if (l>=mid+1) return _count(k*2+1,l,r);    else        return  _count(k*2,l,mid)+_count(k*2+1,mid+1,r);}int   numl[1000002],numr[1000002],sum[1000002];int main(){    memset(tree,0,sizeof(tree));    int n,a[1000002];  long long ans=0;    scanf("%d",&n);    for (int i=1;i<=n;i++) scanf("%d",&a[i]);    int cnt=1;    for (int i=1;i<=n;i++)    {        if (!m1[a[i]]) m1[a[i]]=cnt++;        a[i]=m1[a[i]];    }    cnt=-1;    for (int i=1;i<=n;i++)    {        sum[a[i]]++;        numl[i]=sum[a[i]];        cnt=max(cnt,sum[a[i]]);    }    build(1,1,cnt);    for (int i=1;i<=n;i++)        numr[i]=sum[a[i]]-numl[i]+1;    for (int i=1;i<=n;i++)    {        _insert(1,numl[i-1],numl[i-1],1);        ans+=(i-_count(1,1,numr[i]));    }    cout <<ans;    return 0;}


0 0
原创粉丝点击