Codeforces #261 D
Codeforces #261 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.
简单的树状数组求逆序数。。。开始怎么也没想到。。。
答案没用long long wa一次
#include <cstring> #include <iostream> #include <algorithm> #include <cstdio> #include <cmath> #include <map> #include <cstdlib> #define M(a,b) memset(a,b,sizeof(a)) using namespace std; int n; int num[]; int savefro[],saveb[]; int ans[]; int flag[]; map<int,int> front,back; int bit[],cnn; int sum(int i) { int res = ; while(i>) { res+=bit[i]; i-=i&-i; } return res; } void add(int i,int x) { while(i<=cnn) { bit[i]+=x; i+=i&-i; } } int main() { while(scanf("%d",&n)==) { front.clear(); back.clear(); M(saveb,); M(savefro,); M(ans,); M(flag,); M(bit,); long long res = ; for(int i = ;i<n;i++) { scanf("%d",&num[i]); } for(int i = ;i<n;i++) { savefro[i] = ++front[num[i]]; } cnn = n; for(int i = n-;i>=;i--) { saveb[i] = ++back[num[i]]; add(saveb[i],); res+=sum(savefro[i-]-); // cout<<' '<<sum(savefro[i-1]-1)<<' '<<savefro[i-1]<<endl; } /*cout<<"savefro"<<endl; for(int i = 0;i<n;i++) { cout<<savefro[i]<<' '; } cout<<endl; cout<<"saveb"<<endl; for(int i = 0;i<n;i++) { cout<<saveb[i]<<' '; } cout<<endl; cout<<"flag"<<endl; for(int i = 0;i<n;i++) { cout<<flag[i]<<' '; } cout<<endl;*/ printf("%I64d\n",res); } return ; }
Codeforces #261 D的更多相关文章
- codeforces #261 C题 Pashmak and Buses(瞎搞)
题目地址:http://codeforces.com/contest/459/problem/C C. Pashmak and Buses time limit per test 1 second m ...
- New Training Table
2014_8_15 CodeForces 261 DIV2 A. Pashmak and Garden 简单题 B. Pashmak and Flowers 简单题 C. P ...
- Codeforces Round #261 (Div. 2)[ABCDE]
Codeforces Round #261 (Div. 2)[ABCDE] ACM 题目地址:Codeforces Round #261 (Div. 2) A - Pashmak and Garden ...
- Codeforces Round #261 (Div. 2) B
链接:http://codeforces.com/contest/459/problem/B B. Pashmak and Flowers time limit per test 1 second m ...
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP
http://codeforces.com/contest/459/problem/E 不明确的是我的代码为啥AC不了,我的是记录we[i]以i为结尾的点的最大权值得边,然后wa在第35 36组数据 ...
- Codeforces Round #261 (Div. 2)459D. Pashmak and Parmida's problem(求逆序数对)
题目链接:http://codeforces.com/contest/459/problem/D D. Pashmak and Parmida's problem time limit per tes ...
- Codeforces Round #261 (Div. 2) - E (459E)
题目连接:http://codeforces.com/contest/459/problem/E 题目大意:给定一张有向图,无自环无重边,每条边有一个边权,求最长严格上升路径长度.(1≤n,m≤3 * ...
- Codeforces Round #261 (Div. 2) B. Pashmak and Flowers 水题
题目链接:http://codeforces.com/problemset/problem/459/B 题意: 给出n支花,每支花都有一个漂亮值.挑选最大和最小漂亮值得两支花,问他们的差值为多少,并且 ...
- Codeforces Round #261 (Div. 2)459A. Pashmak and Garden(数学题)
题目链接:http://codeforces.com/problemset/problem/459/A A. Pashmak and Garden time limit per test 1 seco ...
随机推荐
- 一个完整的TCP连接
当我们向服务器发送HTTP请求,获取数据.修改信息时,都需要建立TCP连接,包括三次握手,四次分手. 什么是TCP连接? 为实现数据的可靠传输,TCP要在应用进程间建立传输连接.它是在两个传输用户之间 ...
- NYOJ 485
A*B Problem 描述 设计一个程序求出A*B,然后将其结果每一位相加得到C,如果C的位数大于等于2,继续将C的各位数相加,直到结果是个一位数k. 例如: 6*8=48: 4+8=12: 1+2 ...
- GO语言总结(1)——基本知识
1.注释(与C++一样) 行注释:// 块注释:/* ... */ 2.标识符 可以这么说,除了数字开头的不允许,符号开头的不允许,关键字不允许,其他的Unicode字符组合都可以.“_33”也可以是 ...
- 简单CSS3实现炫酷读者墙
如题,给大家介绍和讲解几个常用的CSS3属性,并用到实处. 先看demo(请使用Chrome或者Firefox浏览,IE的靠边): 点此查看实例 觉得爽的可以继续阅读下面的知识点,感觉不爽的可绕行. ...
- echo print() print_r() var_dump()的区别
常用调试方法 echo()可以一次输出多个值,多个值之间用逗号分隔.echo是语言结构(language construct),而并不是真正的函数,因此不能作为表达式的一部分使用. print()函数 ...
- 获取用户SID
BOOL GetUserSID(CString &Sid) { HANDLE hProcess = GetCurrentProcess(); if (hProcess) { HANDLE hT ...
- php根据地址的经纬度查询周围的城市例子
目前的工作是需要对用户的一些数据进行分析,每个用户都有若干条记录,每条记录中有用户的一个位置,是用经度和纬度表示的.还有一个给定的数据库,存储的是一些已知地点以及他们的经纬度,内有43W多条的数据.现 ...
- Local gulp not found in.. on windows
当出现报错时,请按如下方式安装 gulp 以下使用国内的淘宝镜像安装: $ # Step 1 $ cnpm install -g gulp $ # Step 2 $ cnpm install --sa ...
- iOS小知识点(UI部分)
1. 父视图通过Tag来找到UIView UIView *targetView = [superView viewWithTag:10];//只在当前视图以及subviews中找,不能再孙子中找. 2 ...
- Python笔记(2)函数
python中一切皆对象,函数也看做对象.函数被函数名所引用,但是同样的他也可以被其他标识符所引用,可以作为参数传递. def f(): return "hi" 可见a引用了函数返 ...