codeforces 459D D. Pashmak and Parmida's problem(离散化+线段树或树状数组求逆序对)
题目链接:
D. Pashmak and Parmida's problem
3 seconds
256 megabytes
standard input
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.
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
题意:f[l,r,x]=在a[l],a[l+1]...a[r]中有多少个a[i]等于x,这道题可以问f[1,i,a[i]]>f[j,n,a[j]]&&1<=i<j<=n的i和j的对数,令pre[i]为a[i]在区间[1,i]中出现的次数,同理nex[j]为a[j]为区间[j,n]中a[j]出现的次数,结果就变成了求sigma{pre[i]和nex[i+1]的逆序对数}i为1到n-1;这时不是单单的一个数组求逆序对数(一个数组求逆序对数可以在归并排序中解决),所以得用线段树或者树状数组,树状数组还不会,等学会了树状数组再来更树状数组的代码;
AC代码:
/*~~~~~~~~线段树的代码~~~~~~~~~~~~~*/
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+;
int n,a[N],pre[N],nex[N];
struct nod
{
int l,r,sum;
};
nod tree[*N];
void build(int node,int le,int ri)
{
tree[node].l=le;
tree[node].r=ri;
tree[node].sum=;
if(le==ri)return ;
int mid=(le+ri)>>;
build(*node,le,mid);
build(*node+,mid+,ri);
tree[node].sum=tree[*node].sum+tree[*node+].sum;
}
int query(int node,int L,int R)
{
if(L<=tree[node].l&&R>=tree[node].r)
{
return tree[node].sum;
}
int mid=(tree[node].l+tree[node].r)>>;
if(R<=mid)return query(*node,L,R);
else if(L>mid)return query(*node+,L,R);
else return query(*node,L,R)+query(*node+,L,R);
}
int update(int node,int num)
{
if(tree[node].l==tree[node].r&&tree[node].l==num)
{
tree[node].sum+=;
return ;
}
int mid=(tree[node].l+tree[node].r)>>;
if(num<=mid)update(*node,num);
else update(*node+,num);
tree[node].sum=tree[*node].sum+tree[*node+].sum;
}
map<int,int>mp1,mp2;
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
mp1[a[i]]++;
pre[i]=mp1[a[i]];//用map进行离散化
}
for(int i=n;i>;i--)
{
mp2[a[i]]++;
nex[i]=mp2[a[i]];
}
long long ans=;
build(,,n+);//建树时建到n+1,避免后面的nex[i]+1>n;
for(int i=;i<=n;i++)
{
if(nex[i]!=n)
ans+=(long long)query(,nex[i]+,n);
update(,pre[i]);
}
cout<<ans<<"\n";
return ;
}
/*~~~~~~~~树状数组的代码~~~~~~~~~~为什么用树状数组还没有线段树的快?*/
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+;
int n,a[N],pre[N],nex[N],sum[N];
int lowbit(int x)
{
return x&(-x);
}
void update(int x)
{
while(x<=n)
{
sum[x]++;
x+=lowbit(x);
}
}
int query(int x)
{
int s=;
while(x>)
{
s+=sum[x];
x-=lowbit(x);
}
return s;
}
map<int,int>mp1,mp2;
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
mp1[a[i]]++;
pre[i]=mp1[a[i]];
}
for(int i=n;i>;i--)
{
mp2[a[i]]++;
nex[i]=mp2[a[i]];
}
long long ans=;
for(int i=n;i>;i--)
{
ans+=(long long)query(pre[i]-);
update(nex[i]);
}
cout<<ans<<"\n";
return ;
}
codeforces 459D D. Pashmak and Parmida's problem(离散化+线段树或树状数组求逆序对)的更多相关文章
- codeforces 540E 离散化技巧+线段树/树状数组求逆序对
传送门:https://codeforces.com/contest/540/problem/E 题意: 有一段无限长的序列,有n次交换,每次将u位置的元素和v位置的元素交换,问n次交换后这个序列的逆 ...
- 【Codeforces 459D】Pashmak and Parmida's problem
[链接] 我是链接,点我呀:) [题意] 定义两个函数 f和g f(i)表示a[1..i]中等于a[i]的数字的个数 g(i)表示a[i..n]中等于a[i]的数字的个数 让你求出来(i,j) 这里i ...
- 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, ...
- 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预处理出 ...
- Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数
E. Infinite Inversions ...
- CodeForces 459D Pashmak and Parmida's problem
Pashmak and Parmida's problem Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- 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); 思路:先从左往右统 ...
- codeforces459D:Pashmak and Parmida's problem
Description Parmida is a clever girl and she wants to participate in Olympiads this year. Of course ...
随机推荐
- 2015年Android开发新技术盘点
又到年末. 利用中午的时间,汇总盘点一下今年Android开发方面的新技术.感觉如今Android开发没有曾经那么纯粹了,出现了非常多新的开发模式. 2015年影响比較普遍的新技术应该就是Materi ...
- 笔试真题解析 ALBB-2015 算法project师实习生机试
1.用十进制计算30!(30的阶乘),将结果转换成3进制进行表示的话,该进制下的结果末尾会有____个0. [解析] 计算N.下3进制结果末尾有多少个0,事实上就是计算3进制中的3被进位多少次,仅仅要 ...
- windows常用dos命令
常用命令: d: 回车 磁盘切换 dir: 查看该目录下所有的文件和文件夹: md: 创建文件加 rd: 删除目录 cd: 进入指定的目录 cd..:回退到上级目录 cd\ :回退到根目录 de ...
- CAFFE学习笔记(三)在VS2013下生成需要的exe文件
如我们所知,CAFFE_ROOT下有一个文件夹叫tools,里面中有许多cpp文件,它们各自有其不同的功能.但是很显然,当我们要完成某样工作时,我们是不能直接用cpp文件的,只能用exe文件.如何利用 ...
- mongo explain分析详解
1 为什么要执行explain,什么时候执行 explain的目的是将mongo的黑盒操作白盒化. 比如查询很慢的时候想知道原因. 2 explain的三种模式 2.1 queryPlanner 不会 ...
- 【python】-- socketserver
socketserver SocketServer服务端内部使用 IO多路复用 以及 “多线程” 和 “多进程” ,从而实现并发处理多个客户端请求.即:每个客户端请求连接到服务器时,Socket服务端 ...
- Spring AOP 学习例子
http://outofmemory.cn/code-snippet/3762/Spring-AOP-learn-example 工作忙,时间紧,不过事情再多,学习是必须的.记得以前的部门老大 ...
- swap 内存不足
参考:https://stackoverflow.com/questions/5682854/why-is-the-linker-terminating-on-me-when-i-build-clan ...
- ARDUINO W5100 WebServer测试
1.直接下载官方的enternet->WebServer代码 /* Web Server A simple web server that shows the value of the anal ...
- Mac下XAMPP环境中安装MySQLdb
环境: Mac OS X. Mac下安装MySQLdb模块着实多了些步骤. 用easy_install或者pip安装时有两大问题,"mysql_config not found"和 ...