题目: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&#39;s problem【离散化+处理+逆序对】的更多相关文章

  1. Codeforces Round #261 (Div. 2)459D. Pashmak and Parmida&#39;s problem(求逆序数对)

    题目链接:http://codeforces.com/contest/459/problem/D D. Pashmak and Parmida's problem time limit per tes ...

  2. CodeForces 459D Pashmak and Parmida's problem

    Pashmak and Parmida's problem Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

  3. CF #261 div2 D. Pashmak and Parmida&#39;s problem (树状数组版)

    Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants he ...

  4. 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); 思路:先从左往右统 ...

  5. 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 ...

  6. codeforces459D:Pashmak and Parmida's problem

    Description Parmida is a clever girl and she wants to participate in Olympiads this year. Of course ...

  7. 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 ...

  8. Codeforces 459E Pashmak and Graph(dp+贪婪)

    题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...

  9. Codeforces Round #367 (Div. 2) C. Hard problem

    题目链接:Codeforces Round #367 (Div. 2) C. Hard problem 题意: 给你一些字符串,字符串可以倒置,如果要倒置,就会消耗vi的能量,问你花最少的能量将这些字 ...

随机推荐

  1. poj 3082多边形相交 'Roid Rage

    题意是判断多边形是否相交 主要的思路就是判断每一个点是否在另外的多变形内 判断一个点是否在另一个多边形内主要思路是: 判断的那个点向左边做射线,如果射线与多边形的交点为奇数个则在多边形内,偶数个则不在 ...

  2. VC 获取指定文件夹路径的方法小结

    VC获取指定文件夹路径 flyfish  2010-3-5 一 使用Shell函数 1 获取应用程序的安装路径 TCHAR buf[_MAX_PATH];SHGetSpecialFolderPath( ...

  3. 两道二分coming~

    第一道:poj 1905Expanding Rods 题意:两道墙(距离L)之间架一根棒子,棒子受热会变长,弯曲,长度变化满足公式( s=(1+n*C)*L),求的是弯曲的高度h. 首先来看这个图: ...

  4. Delphi xe7并行编程快速入门(三篇)

    现在多数设备.计算机都有多个CPU单元,即使是手机也是多核的.但要在开发中使用多核的优势,却需要一些技巧,花费时间编写额外的代码.好了,现在可以使用Delphi做并行编程了. 在Delphi.C++ ...

  5. Android installed app, never used, cannot receiver BroadcastReceiver

    官方文档是这么写的:(http://developer.android.com/about/versions/android-3.1.html#launchcontrols) Launch contr ...

  6. Android获取设备採用的时间制式(12小时制式或24小时制式)

    /** * 获取设备採用的时间制式(12小时制式或者24小时制式) * 注意: * 在模拟器上获取的时间制式为空 */ private void getTime_12_24(Context conte ...

  7. 【剑指offer】和为定值的两个数

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/24933341 题目描写叙述: 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的 ...

  8. [Android]ADT Run时候报错:The connection to adb is down, and a severe error has occured

    The connection to adb is down, and a severe error has occured. 之 ..\sdk\platform-tools\adb.exe and c ...

  9. Config File Settings Of EF——实体框架的配置文件设置

    我亦MSDN 原文地址 http://msdn.microsoft.com/en-us/data/jj556606 Entity Framework allows a number of settin ...

  10. 在qt中用tcp传输xml消息

    在qt中用tcp传输xml消息 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 主机:WIN7 开发环境:Qt5 3.1.2 说明: 在tcp上 ...