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的能量,问你花最少的能量将这些字 ...
随机推荐
- centos 安装ganglia监控工具
一个.ganglia基本介绍 ganglia它是一个分布式监控系统,那里有两个Daemon,每间:clientGangliaMonitoring Daemon (gmond)和服务端GangliaMe ...
- ios html5 设定PhoneGap开发环境
怎么样IOS平台搭建PhoneGap开发环境(PhoneGap2.5) (2013-03-13 14:44:51) 标签: c=blog&q=it&by=tag" targe ...
- Photoshop图象切片保存为网页HTML(DIV+CSS布局)的方法
首先,制作图象切片(以一张图片为例子) 一.选择“切片”工具,在图像上拖动以分割图像(例如:一张图像切割2次就形成3个切片)切片后如下图 二.设置切片选项(如大小.目标链接.图片说明等等):选择“切片 ...
- hadoop namanodejava
最近突然想看下hadoop源码,有利于处理一些突发问题.先从name启动开始, NameNode.java public static void main(String argv[]) throws ...
- (二)----HTTP请求头与响应头
一.HTTP头引入: 正确的设置HTTP头部信息有助于搜索引擎判断网页及提升网站访问速度.通常HTTP消息包括:客户机向服务器的请求消息和服务器向客户机的响应消 息.客户端向服务器发送一个请求,请求头 ...
- The mmap module
The mmap module The mmap module (New in 2.0) This module provides an interface to the operating syst ...
- Linux 技巧之 Grub 超实用技巧
1. 简单介绍 什么是 GRUB?GRUB 全名Grand Unified Boot Loader,它是一个引导装入器 -- 它负责装入内核并引导 Linux 系统.GRUB 还能够引导其他操作系统, ...
- HDU1176:免费馅饼(DP)
Problem Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁 ...
- js关闭浏览器窗口事件
js关闭浏览器窗口 js关闭浏览器窗口,不弹出提示框.支持ie6+,火狐,谷歌等浏览器. <html> <head /> <body> <script typ ...
- hdu1151+poj2594(最小路径覆盖)
传送门:hdu1151 Air Raid 题意:在一个城镇,有m个路口,和n条路,这些路都是单向的,而且路不会形成环,现在要弄一些伞兵去巡查这个城镇,伞兵只能沿着路的方向走,问最少需要多少伞兵才能把所 ...