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的能量,问你花最少的能量将这些字 ...
随机推荐
- OCA读书笔记(2) - 安装Oracle软件
Objectives: •Describe your role as a database administrator (DBA) and explain typical tasks and tool ...
- 联系人数据库设计之ContactsTransaction
不当之处,请雅正. 请自行下载android源代码 package com.android.providers.contacts; import com.google.android.collect. ...
- MySQL 创建函数(Function)
目标 怎么样MySQL创建数据库功能(Function) 语法 CREATE FUNCTION func_name ( [func_parameter] ) //括号是必须的,參数是可选的 RETUR ...
- 细说UI线程和Windows消息队列
在 Windows应用程序中,窗体是由一种称为“ UI线程( User Interface Thread)”的特殊类型的线程创建的. 首先, UI线程是一种“线程”,所以它具有一个线程应该具有的所有特 ...
- 【ASP.NET Web API教程】4.3 ASP.NET Web API中的异常处理
原文:[ASP.NET Web API教程]4.3 ASP.NET Web API中的异常处理 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面的内 ...
- MySQL中同一时候存在创建和上次更新时间戳字段解决方法浅析
在写这篇文章之前.明白我的MySQL版本号. mysql> SELECT VERSION(); +------------+ | VERSION() | +------------+ | 5.5 ...
- .NET Core 1.0.0 RC2
.NET Core 1.0.0 RC2 在.NET Core 1.0.0 RC2即将正式发布之际,我也应应景,针对RC2 Preview版本编写一个史上最简单的MVC应用.由于VS 2015目前尚不支 ...
- Hangfire Highlighter Tutorial
Hangfire Highlighter Tutorial Hangfire是一个开源且商业免费使用的工具函数库.可以让你非常容易地在ASP.NET应用(也可以不在ASP.NET应用)中执行多种类型的 ...
- Activity组件的生命周期
一.Activiy组件的三个状态: 1.前台状态(active) : 在屏幕的最上层,页面获得焦点,可以响应用户的操作2.可视状态(paused) : 不能与用户交互,但是还存在于可视区域内,它依然存 ...
- TWinControl的消息覆盖函数大全(41个WM_函数和31个CM_函数,它的WndProc就处理鼠标(转发)、键盘(取消拖动)、焦点、和WM_NCHITTEST一共4类消息)
注意,这些函数只有Private一种形式(也就是不允许覆盖,但仍在动态表格中): 其中TWinControl对TControl有10个消息进行了覆盖(红色标记),其中有2个是WM_消息,8个是CM_消 ...