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 ...
随机推荐
- 多媒体开发之rtp 打包发流---同网段其他机子sdp 播放不了
(1) (2) (3) -------------author:pkf ------------------time:2015-1-6 后面发现是connection 的server 地址是指定的 导 ...
- asp.net core使用中间件美化开发环境异常页面
asp.net core系统自带的异常页面色彩给人感觉模糊.朦胧,晕眩! 原版: 美化版 实现思路:(在系统自带异常中间件“DeveloperExceptionPageMiddleware”执行后,调 ...
- IOS启动页动画(uiview 淡入淡出效果 )2
Appdelegate里面右个这个函数,只要它没结束,你的等待界面就不会消失.以在启动的时候做些动画 - (BOOL)application:(UIApplication *)application ...
- TP5.0中的小知识总结
2017年6月26日15:01:231.input 获取输入数据 支持默认值和过滤:接收用户在前台输入的数据,可以是get方式也可以是post方式.2.ThinkPHP5.0内置了分页实现,要给 ...
- PHPstudy如何在本地搭建多站点
参考地址: http://jingyan.baidu.com/article/e52e36154227ef40c70c5147.html
- Unable to determine IP address from host name
- Mac标识物理位置算法 import Levenshtein mac列表特征值
mac 字符串 与 基准字符串的 Levenshtein 距离,考虑 mac信号强度的时序性,60秒内若干次变化 不引入强度 mac字符串的唯一性 如何排序 基准字符串的选取 同一尺度 都按强度 ...
- 18.Django原生SQL语句查询返回字典
在django中执行自定义语句的时候,返回的结果是一个tuple ,并我不是我所期望的dict.当结果是tuple 时,如果要取得数据,必须知道对应数据在结果集中的序号,用序号的方式去得到值. 如果是 ...
- spring项目改名后不能启动的原因及解决办法
今日修改了一个spring项目的项目名称,修改后启动项目Debug as->Debug on server,过了很久也没有出现web首页,仔细看项目的定时器已经启动,eclipse的Consol ...
- 读取ByteBuffer有效的数据
转:https://zhidao.baidu.com/question/427134449349230532.html说道 ByteBuffer的缓冲区,就需要知道缓冲区的的三个状态1)capacit ...