Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem
来源:互联网 发布:手机录音播放软件 编辑:程序博客网 时间:2024/05/22 03:30
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.
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).
Print a single integer — the answer to the problem.
71 2 1 1 2 2 1
8
31 1 1
1
51 2 3 4 5
0
大致思路,类比了求逆序数:
1、先离散化,因为数据范围达到了1e9
2、然后处理出ai[] aj[]数组,其含义注释,对ai[]建立树状数组处理,然后用aj[]倒着查询,最大是(1e6)^2 /2 所以要开long long的
#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <iostream>#include <iomanip>#include <cmath>#include <map>#include <set>#include <queue>using namespace std;#define ls(rt) rt*2#define rs(rt) rt*2+1#define ll long long#define ull unsigned long long#define rep(i,s,e) for(int i=s;i<e;i++)#define repe(i,s,e) for(int i=s;i<=e;i++)#define CL(a,b) memset(a,b,sizeof(a))#define IN(s) freopen(s,"r",stdin)#define OUT(s) freopen(s,"w",stdout)const ll ll_INF = ((ull)(-1))>>1;const double EPS = 1e-8;const int INF = 1e9+100;const int MAXN = 1e6+100;inline int lowb(int x){return x&(-x);}ll c[MAXN];ll tmp[MAXN],a[MAXN],xcnt[MAXN],ycnt[MAXN];ll ai[MAXN],aj[MAXN];bool cmp(const int i, const int j){ return a[i]<a[j];}int n;void dis(){ sort(tmp+1,tmp+1+n,cmp); int pre=a[tmp[1]]-1,st=0; for(int i=1;i<=n;i++) { if(a[tmp[i]]!=pre) { pre=a[tmp[i]]; a[tmp[i]]=++st; } else a[tmp[i]]=st; }}void add(int x, int d){ while(x<=n) { c[x]+=d; x+=lowb(x); }}int sum(int x){ int ret=0; while(x>0) { ret+=c[x]; x-=lowb(x); } return ret;}int main(){ //IN("D.txt"); while(~scanf("%d",&n)) { for(int i=1;i<=n;i++) scanf("%I64d",&a[i]),tmp[i]=i; dis(); CL(xcnt,0);CL(ycnt,0); // int mmax=0; for(int i=1;i<=n;i++) { xcnt[a[i]]++; ai[i]=xcnt[a[i]]; // ai[i] i之前有几个a[i] //mmax=max(mmax,ai[i]); } CL(c,0); ll ans=0; ///////////////// //for(int i=1;i<=n;i++) // printf("a[%d]=%d ai=%d\n",i,a[i],ai[i]); ////////////////// for(int i=n;i>1;i--)/// { ycnt[a[i]]++; aj[i]=ycnt[a[i]]; // aj[i] i之后有介个a[i] add(aj[i],1); ans+=sum(ai[i-1]-1); } printf("%I64d\n",ans); } return 0;}
- Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem
- Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem
- Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem(树状数组+逆序数对)
- Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem
- Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem
- Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem 离散化+树状数组
- Codeforces Round 261 Div.2 D Pashmak and Parmida's problem --树状数组
- Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem (树状数组)
- Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem
- Codeforces Round #261 (Div. 2)459D. Pashmak and Parmida's problem(求逆序数对)
- Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem (树状数组求逆序数 变形)
- 【CODEFORCES】 D. Pashmak and Parmida's problem
- Codeforces #261 (Div. 2) D. Pashmak and Parmida's problem(数据结构:离散化+线段树+逆序数+二分查找)
- Codeforces Round 459 D. Pashmak and Parmida's problem 树状数组求逆序数 变形
- Codeforces 459D Pashmak and Parmida's problem(树状数组)
- codeforces 459D Pashmak and Parmida's problem 树状数组
- Codeforces 459D Pashmak and Parmida's problem
- CodeForces 459D Pashmak and Parmida's problem
- CentOS 6.3上SVN安装过程及使用手册
- A*(路径搜索)算法
- 类继承几点常见注意事项
- 普里姆算法(Prim算法)C - 畅通工程再续(再看)
- JSONObject jsonObject = new JSONObject()执行错误
- Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem
- Apollo 入门指导
- hdu1251-统计难题
- 【MAC XAMPP】mysql的启动 与 PS命令的详解
- CentOS 6.5 Varnish缓存服务详解及应用实现
- 最小生成树,转别人的
- 杭电1421 搬寝室(dp)
- C# winForm启动最小化到任务栏右侧通知栏并交互操作
- 算法笔记---最短路径之bellman-ford算法