Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem
2015-07-25 14:26
411 查看
题目传送门:http://codeforces.com/contest/459/problem/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.
Sample test(s)
input
output
input
output
input
output
题意:给一个长度为n(n<1e6)的数组,并定义 f(l, r, x)为l到r这一段数组中等于x的个数。
求i,j的组数使得 f(1, i, ai) > f(j, n, aj)
题解:首先用Map,nlogn的方法求出所有的 f(1, i, ai) 和f(j, n, aj),
然后从左到右或者从右到左扫描一遍,用线段树或者树状数组记录每个数字出现的个数,然后求解即可。算是比较简单的数据结构题,没什么值得细讲的
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.
Sample test(s)
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
题意:给一个长度为n(n<1e6)的数组,并定义 f(l, r, x)为l到r这一段数组中等于x的个数。
求i,j的组数使得 f(1, i, ai) > f(j, n, aj)
题解:首先用Map,nlogn的方法求出所有的 f(1, i, ai) 和f(j, n, aj),
然后从左到右或者从右到左扫描一遍,用线段树或者树状数组记录每个数字出现的个数,然后求解即可。算是比较简单的数据结构题,没什么值得细讲的
#include <iostream> #include <string> #include <stdio.h> #include <algorithm> #include <string.h> #include <map> using namespace std; int a[1000005]; int x; int l[1000005]; int r[1000005]; long long ans; map<int,int>m; long long s[1000005*4]; void query(int l,int r,int o) { if (r<x) ans+=s[o]; if (r<x) return ; if (l>x) return ; if (l==r) return ; int mid=(l+r)/2; query(l,mid,2*o); query(mid+1,r,2*o+1); return ; } void add(int l,int r,int o) { if (l<=x&&x<=r) s[o]++; if (r<x) return ; if (l>x) return ; if (l==r) return ; int mid=(l+r)/2; add(l,mid,2*o); add(mid+1,r,2*o+1); return ; } int main() { int n; while (~scanf("%d",&n)) { for (int i=1;i<=n;i++)scanf("%d",&a[i]); m.clear();for (int i=1;i<=n;i++) {m[a[i]]++;l[i]=m[a[i]];} m.clear();for (int i=n;i>=1;i--) {m[a[i]]++;r[i]=m[a[i]];} memset(s,0,sizeof(s)); ans=0; for (int i=n;i>=1;i--) { x=l[i]; query(1,n,1); x=r[i]; add(1,n,1); } cout<<ans<<endl; } return 0; }
相关文章推荐
- Wolf and Rabbit
- ios下inhouse发布相关代码
- HDU 4507 吉哥系列故事——恨7不成妻(数位DP)
- UVa 11134 - Fabled Rooks(贪心)
- poj 2570 Fiber Network(floyd)
- 黑马程序员——集合框架(Map详解)
- 从源代码分析modelDriven拦截器和params拦截器和拦截器prepare 和paramsPrepareParamsStack拦截器栈(让你的Struts2代码更简洁——如何培养框架设计能力
- poj 2570 Fiber Network(floyd)
- [黑马程序员](第16-22天)IO流(下)
- iOS __weak学习碰到的疑问
- POJ 2251 Dungeon Master
- ProgressDialog使用总结
- JBOSS ESB企业服务器总线
- [黑马程序员](第16-22天)IO流(上)
- [黑马程序员](第16-22天)IO流(中)
- myeclipse 2015 安装 aptana3.6
- 教你如何解包、打包boot.img以及获取adb root权限
- 每天一个Linux命令-9(Find–atime –ctime –mtime)
- 卡特兰数
- android studio 运行代码时device不能识别genymotion设备