codeforces459D:Pashmak and Parmida's problem
Description
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.
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.
7
1 2 1 1 2 2 1
8
3
1 1 1
1
5
1 2 3 4 5
0 正解:离散化+树状数组
解题报告:
首先离散化之后,可以预处理一下每个元素的前驱相等元素个数和后继相等元素个数,O(NlogN)
之后我们可以得到每个元素的两个值,前驱个数值pre[i]和后继个数值next[i],我们的任务就是查找i和j满足pre[i]>next[j] && i<j的个数。
感觉是不是很像逆序对?直接从后往前往树状数组中插入next值(记得上界是n,处理一下0),每次查找比当前pre小的next个数,因为我们是从后往前插入的next,所以可以保证i<j
codeforces的评测机真快,100w数据0.4S,丝毫不虚
//It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int MAXN = ;
int n,a[MAXN],u[MAXN];
int pre[MAXN],next[MAXN];
int jump1[MAXN],jump2[MAXN];
bool vis[MAXN];
int shu[MAXN],L;//树状数组维护共有多少个小于他的数
LL ans;
struct node{
int val,id;
}b[MAXN]; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline bool cmp(node q,node qq){ if(q.val==qq.val) return q.id<qq.id; return q.val<qq.val; } inline void add(int x,int val){
while(x<=n+) {
shu[x]+=val;
x+=x&(-x);
}
} inline int query(int x){
int total=;
while(x) {
total+=shu[x];
x-=x&(-x);
}
return total;
} inline void work(){
n=getint(); for(int i=;i<=n;i++) u[i]=b[i].val=a[i]=getint(),b[i].id=i,jump2[i]=n+;
sort(b+,b+n+,cmp);
for(int i=;i<=n;i++) if(b[i].val==b[i-].val) jump1[b[i].id]=b[i-].id,jump2[b[i-].id]=b[i].id;
sort(u+,u+n+);
L=unique(u+,u+n+)-u-;
for(int i=;i<=n;i++) a[i]=lower_bound(u+,u+L+,a[i])-u;
int x;
for(int i=;i<=n;i++) {
if(vis[i]) continue; vis[i]=;
x=i; while(x<=n) x=jump2[x],pre[x]=pre[jump1[x]]+,vis[x]=; }
memset(vis,,sizeof(vis));
for(int i=n;i>=;i--) {
if(vis[i]) continue; vis[i]=;
x=i; while(x) x=jump1[x],next[x]=next[jump2[x]]+,vis[x]=;
} for(int i=n;i;i--) {
ans+=query(pre[i]);
add(next[i]+,);
}
printf("%lld",ans);
} int main()
{
work();
return ;
}
codeforces459D:Pashmak and Parmida's problem的更多相关文章
- CodeForces 459D Pashmak and Parmida's problem
Pashmak and Parmida's problem Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- codeforces 459D D. Pashmak and Parmida's problem(离散化+线段树或树状数组求逆序对)
题目链接: D. Pashmak and Parmida's problem time limit per test 3 seconds memory limit per test 256 megab ...
- cf459D Pashmak and Parmida's problem
D. Pashmak and Parmida's problem time limit per test 3 seconds memory limit per test 256 megabytes i ...
- codeforces D. Pashmak and Parmida's problem
http://codeforces.com/contest/459/problem/D 题意:给你n个数,然后统计多少组(i,j)使得f(1,i,ai)>f(j,n,aj); 思路:先从左往右统 ...
- codeforces 459 D. Pashmak and Parmida's problem(思维+线段树)
题目链接:http://codeforces.com/contest/459/problem/D 题意:给出数组a,定义f(l,r,x)为a[]的下标l到r之间,等于x的元素数.i和j符合f(1,i, ...
- Pashmak and Parmida's problem(树状数组)
题目链接:http://codeforces.com/contest/459/problem/D 题意: 数列A, ai表示 i-th 的值, f(i,j, x) 表示[i,j]之间x的数目, 问:当 ...
- CF459D Pashmak and Parmida's problem (树状数组)
Codeforces Round #261 (Div. 2) 题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a ...
- Codeforces Round 261 Div.2 D Pashmak and Parmida's problem --树状数组
题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求有多少对这样的(i,j). 解法:分别从左到右,由右到 ...
- Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem (树状数组求逆序数 变形)
题目链接 题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求i和j的种类数. 我们可以用map预处理出 ...
随机推荐
- 【BZOJ3239】Discrete Logging BSGS
[BZOJ3239]Discrete Logging Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B ...
- 160819、JavaScript-数组去重由慢到快由繁到简
JavaScript-数组去重由慢到快由繁到简演化 indexOf去重 Array.prototype.unique1 = function() { var arr = []; for (var ...
- IOS开发复习笔记(3)-ARC
1.ARC 当你自己调用了release或retain语句的时候,ARC有效时编译文件会遇到错误,你可以通过-fno-objc-arc和-fobjc-arc两个编译器标志在混搭中支持ARC和非ARC的 ...
- getDomain(url)-我的JavaScript函数库-mazey.js
获取链接地址中域名,如mazey.net,www.mazey.net,m.mazey.net. 参数:url 必需function getDomain(url){ var a = documen ...
- 洛谷 P3263 [JLOI2015]有意义的字符串
洛谷 首先,看到\((\frac{(b+\sqrt{d})}{2})^n\),很快能够想到一元二次方程的解\(\frac{-b\pm\sqrt{\Delta}}{2a}\). 所以可以推出,\(\fr ...
- How To Surf The Internet In Right Ways
本文偏指导性质,具体实现自行探索~~ 科普 如何***既然想学点东西,就不能被网络束缚住.国内的网络环境,对于外面世界探索还是挺限制的. 什么是墙GFW(great firewall) 中国特有的.就 ...
- Python3.6全栈开发实例[027]
27.文件a.txt内容:每一行内容分别为商品名字,价钱,个数.apple 10 3tesla 100000 1mac 3000 2lenovo 30000 3chicken 10 3通过代码,将其构 ...
- Python3.6全栈开发实例[024]
24.文件a1.txt内容(注意每行中的空格是不一样的,需要对空格进行处理)序号 部门 人数 平均年龄 备注 1 python 30 26 单身狗 2 Linu ...
- Python3.6全栈开发实例[023]
23.税务部门征收所得税. 规定如下: (1)收入在2000以下的. 免征. (2)收入在2000-4000的, 超过2000部分要征收3%的税. (3)收入在4000-6000的, 超过4000部分 ...
- Clustered and Secondary Indexes
Clustered and Secondary Indexes secondary index A type of InnoDB index that represents a subset of t ...