链接:http://codeforces.com/problemset/problem/459/D

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 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.

Examples
input
7
1 2 1 1 2 2 1
output
8
input
3
1 1 1
output
1
input
5
1 2 3 4 5
output
0

题意:f(1,i,ai)表示(1,i)中等于ai的个数,f(j,n,aj)表示(j,n)中与aj相等的个数,求有多少对(i,j)。(特么,之前题意看错了,想了一上午没想明白,结果。。。)
思路:可以用数组fro统计每个位置(包括该位置)其前面有多少个与它相等的数,用beh数组统计每个位置其后有多少个与它相等的数。
  样例:
    7
    1 2 1 1 2 2 1
    fro:{1,1,2,3,2,3,4}  
    beh:{4,3,3,2,2,1,1} 问题就转化为求第一个序列相对第二个序列的逆序数(因为相当于i可以从fro中查看,j可以从beh中查看)
线段树求逆序数。。。
可以从前往后处理,也可以从后往前处理。
貌似从后往前更好理解。
从后往前处理,即,将beh依次插入树中,每插一个,查询其对应的fro的元素(1,fro[i]-1)区间也就是小于fro[i]的个数(查当前树中比fro[i]小的个数)
从前往后跟以上思路相反,插fro查beh(差当前树中比beh[i]大的个数)。
代码中第一次接触到离散化,以后可以借鉴。
此题还需多加理解。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<map>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
struct Node
{
int l,r;
int num;
} tree[<<]; int num[];
int fro[],beh[],sum[];
map<int,int> m1; void build(int l,int r,int rt)
{
tree[rt].l=l;
tree[rt].r=r;
tree[rt].num=;
if(tree[rt].l==tree[rt].r)
return;
int mid=(l+r)/;
build(lson);
build(rson);
} void update(int x,int l,int r,int rt)
{
tree[rt].num++;
if(tree[rt].l==tree[rt].r&&tree[rt].l==x)
return;
int mid=(l+r)/;
if(x<=mid)
update(x,lson);
else
update(x,rson);
} int query(int L,int R,int l,int r,int rt)
{
if(L>R)
return ;
if(L==l&&R==r)
return tree[rt].num;
int mid=(l+r)/;
if(R<mid)
return query(L,R,lson);
else if(L>mid)
return query(L,R,rson);
else
return query(L,mid,lson)+query(mid+,R,rson); }
int main()
{
int n;
scanf("%d",&n);
for(int i=; i<=n; i++)
scanf("%d",&num[i]);
int cnt=;
for(int i=; i<=n; i++) ///离散化,以后可以借鉴
{
if(!m1[num[i]])
m1[num[i]]=cnt++;
num[i]=m1[num[i]];
}
cnt=;
memset(sum,,sizeof(sum));
for(int i=; i<=n; i++)
{
sum[num[i]]++;
fro[i]=sum[num[i]];
cnt=max(cnt,fro[i]);
}
memset(sum,,sizeof(sum));
for(int i=n; i>=; i--)
{
sum[num[i]]++;
beh[i]=sum[num[i]];
}
build(,cnt,);
long long ans=;
for(int i=n; i>; i--)
{
update(beh[i],,cnt,);
//cout<<"**"<<endl;
ans+=query(,fro[i-]-,,cnt,);
//cout<<ans<<'*'<<endl;
}
printf("%I64d\n",ans);
return ;
}

最新文章

  1. 神奇的Bank系统之旅哦
  2. Get it,你离几何达人不远了!
  3. Jquery.Datatables 导出excel
  4. MyEclipse下创建的项目导入到Eclipse中详细的图文配置方法
  5. 修改host文件屏蔽视频广告和网站
  6. struts2访问servlet API
  7. idea自动生成serialVersionUID
  8. Linux下的crontab定时执行任务命令详解
  9. Delphi XE5教程12:注释和编译器指示字
  10. docker 服务注册
  11. 07.15 first与first-child的区别
  12. 【深度学习系列】用PaddlePaddle和Tensorflow进行图像分类
  13. 【OCR技术系列之一】字符识别技术总览
  14. [置顶] Xamarin android中使用signalr实现即时通讯
  15. 开发Spring过程中几个常见异常(一)
  16. Fiddler-学习笔记-远程抓包
  17. 为什么90%的CTO 都做不好绩效管理
  18. select * from dim.dim_area_no@to_dw
  19. ServiceDesk Plus服务管理软件,减轻帮助台负荷,提高IT效率
  20. android sqlite3:数据库操作

热门文章

  1. Office Excel找不到PERSONAL.XLS怎么办
  2. 云计算VDI相关职位招聘
  3. webpack-入口篇
  4. javascript遍历数组的两种方法
  5. MessageBox.Show
  6. hdoj--5526--欧拉回路(欧拉回路)
  7. 前端性能调优Gzip Filter
  8. jsp制作登录正在加载页面.....
  9. Python基础:一起来面向对象 (一)
  10. 所有版本chrome、chromedriver、firefox下载链接