codeforces 459D - Pashmak and Parmida's problem【离散化+处理+逆序对】
题目:codeforces 459D - Pashmak and Parmida's problem
题意:给出n个数ai。然后定义f(l, r, x) 为ak
= x,且l<=k<=r,的k的个数。求 i, j (1 ≤ i < j ≤ n) ,f(1, i, ai) > f(j, n, aj).,有多少对满足条件的i。j。
分类:逆序数。线段树。离散化,
分析:这是一道不错的数据结构题目,比較灵活。
推一下第一组例子之后发现时让求一个逆序数的题目。可是不是单纯的求逆序数。
第一组例子:
1 2 1 1 2 2 1
然后我们按数的出现的次数从前往后编号。得到:
1 1 2 3 2 3 4
在从后往前编号:得到
4 3 3 2 2 1 1
然后我们从第二组数中的数在第一组数中找逆序对就是ans。
当前给出的数是1e-9次方。所以要先离散化一次,然后能够用线段树求逆序数的方法就ok。要注意的是ans会超int
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int N = 1001000;
int a[N],b[N],c[N],sum[N];
struct Node
{
int l,r,num;
};
Node tree[4*N];
map<int,int> m1,m2;
void build(int l,int r,int o)
{
tree[o].l=l,tree[o].r=r;
tree[o].num=0;
if(l==r)
return ;
int mid=(l+r)>>1;
build(l,mid,o<<1);
build(mid+1,r,o+o+1);
}
void update(int t,int o)
{
if(tree[o].l==tree[o].r && tree[o].l==t)
{
tree[o].num++;
return ;
}
int mid=(tree[o].l+tree[o].r)>>1;
if(t>mid)
update(t,o+o+1);
else
update(t,o+o);
tree[o].num=tree[o+o].num+tree[o+o+1].num;
}
int query(int l,int r,int o)
{
//printf("%d %d %d %d\n",l,r,tree[o].l,tree[o].r);
if(tree[o].l==l && tree[o].r==r)
{
return tree[o].num;
}
int mid=(tree[o].l+tree[o].r)>>1;
if(r<=mid)
return query(l,r,o+o);
else if(l>mid)
return query(l,r,o+o+1);
else
return query(l,mid,o*2)+query(mid+1,r,o*2+1);
}
int main()
{
//freopen("Input.txt","r",stdin);
int n;
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int cnt=1;
for(int i=0;i<n;i++) //离散化
{
if(!m1[a[i]])
m1[a[i]]=cnt++;
a[i]=m1[a[i]];
}
cnt = 0;
memset(sum,0,sizeof(sum));
for(int i=0;i<n;i++)
{
sum[a[i]]++;
b[i]=sum[a[i]];
cnt=max(cnt,b[i]);
}
memset(sum,0,sizeof(sum));
for(int i=n-1;i>=0;i--)
{
sum[a[i]]++;
c[i]=sum[a[i]];
}
build(1,cnt,1);
long long ans=0;
for(int i=0;i<n;i++)
{
if(c[i]<cnt)
ans+=query(c[i]+1,cnt,1);
update(b[i],1);
}
printf("%lld\n",ans);
m1.clear();
m2.clear();
}
return 0;
}
codeforces 459D - Pashmak and Parmida's problem【离散化+处理+逆序对】的更多相关文章
- 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 459D Pashmak and Parmida's problem
Pashmak and Parmida's problem Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- CF #261 div2 D. Pashmak and Parmida's problem (树状数组版)
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants he ...
- 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 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 ...
- codeforces459D:Pashmak and Parmida's problem
Description Parmida is a clever girl and she wants to participate in Olympiads this year. Of course ...
- 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 459E Pashmak and Graph(dp+贪婪)
题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...
- Codeforces Round #367 (Div. 2) C. Hard problem
题目链接:Codeforces Round #367 (Div. 2) C. Hard problem 题意: 给你一些字符串,字符串可以倒置,如果要倒置,就会消耗vi的能量,问你花最少的能量将这些字 ...
随机推荐
- WinExec函数,启动其他应用程序
WinExec The WinExec function runs the specified application. Note This function is provided only fo ...
- [C#基础] 泛型
为什么泛型? 在泛型中,最重要的应用便是集合类,因此我们模拟一个简单的集合类 对于上述示例,可以有如下应用 从上可看出,自定义的代码太丑陋了,只能用于string类型. 当然我们可以用object作为 ...
- ASP.NET常被忽视的一些细节
原文:ASP.NET常被忽视的一些细节 前段时间碰到一个问题:为什么在ASP.NET程序中定时器有时候会不工作? 这个问题看起来很奇怪,代码好像也没错,但就是结果与预期不一致. 其实这里是ASP.NE ...
- VC调试技巧
Visual C++ 的 C 运行时刻函数库标识模板0xCD 已经分配的数据(alloCated Data)0xDD 已经释放的数据(Deleted Data)0xFD 被保护的数据 ...
- Disqus – About Disqus
Disqus – About Disqus Disqus is a free service that enables great online communities. As the web's ...
- C语言信号学习笔记
在C语言中,对于错误有很多处理方式.然而,今天学习了信号处理,感觉这种处理方式十分灵活,特此记录. 关于信号处理的函数包含于头文件<signal.h>中.所谓的信号,多指出乎程序员意料的行 ...
- Automatic logon configuration on Linux OS
Automatic logon configuration on Linux OS 1. Regarding to DSA: a) ssh-keygen -t dsa b) cat ~/.ssh/i ...
- Java NIO实战之聊天室
在工作之余花了两个星期看完了<Java NIO>.整体来说这本书把NIO写的非常具体,没有过多的废话,讲的都是重点,仅仅是翻译的中文版看的确实吃力.英文水平太低也没办法,总算也坚持看完了. ...
- FindWindowEx使用方法
函数功能:该函数获得一个窗体的句柄,该窗体的类名和窗体名与给定的字符串相匹配.这个函数查找子窗体,从排在给定的子窗体后面的下一个子窗体開始.在查找时不区分大写和小写. 函数原型:HWND FindWi ...
- CentOS 6.4下编译安装MySQL 5.6.16
一.卸载旧版本号MySql 1.rpm卸载: 1> 检查安装包: rpm -qa | grep mysql 2> 普通删除: rpm -e mysql-5.6.16.rpm 3> 强 ...