codeforces459D:Pashmak and Parmida's problem
Description
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 and ak = 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 正解:离散化+树状数组
解题报告:
首先离散化之后,可以预处理一下每个元素的前驱相等元素个数和后继相等元素个数,O(NlogN)
之后我们可以得到每个元素的两个值,前驱个数值pre[i]和后继个数值next[i],我们的任务就是查找i和j满足pre[i]>next[j] && i<j的个数。
感觉是不是很像逆序对?直接从后往前往树状数组中插入next值(记得上界是n,处理一下0),每次查找比当前pre小的next个数,因为我们是从后往前插入的next,所以可以保证i<j
codeforces的评测机真快,100w数据0.4S,丝毫不虚
//It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int MAXN = ;
int n,a[MAXN],u[MAXN];
int pre[MAXN],next[MAXN];
int jump1[MAXN],jump2[MAXN];
bool vis[MAXN];
int shu[MAXN],L;//树状数组维护共有多少个小于他的数
LL ans;
struct node{
int val,id;
}b[MAXN]; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline bool cmp(node q,node qq){ if(q.val==qq.val) return q.id<qq.id; return q.val<qq.val; } inline void add(int x,int val){
while(x<=n+) {
shu[x]+=val;
x+=x&(-x);
}
} inline int query(int x){
int total=;
while(x) {
total+=shu[x];
x-=x&(-x);
}
return total;
} inline void work(){
n=getint(); for(int i=;i<=n;i++) u[i]=b[i].val=a[i]=getint(),b[i].id=i,jump2[i]=n+;
sort(b+,b+n+,cmp);
for(int i=;i<=n;i++) if(b[i].val==b[i-].val) jump1[b[i].id]=b[i-].id,jump2[b[i-].id]=b[i].id;
sort(u+,u+n+);
L=unique(u+,u+n+)-u-;
for(int i=;i<=n;i++) a[i]=lower_bound(u+,u+L+,a[i])-u;
int x;
for(int i=;i<=n;i++) {
if(vis[i]) continue; vis[i]=;
x=i; while(x<=n) x=jump2[x],pre[x]=pre[jump1[x]]+,vis[x]=; }
memset(vis,,sizeof(vis));
for(int i=n;i>=;i--) {
if(vis[i]) continue; vis[i]=;
x=i; while(x) x=jump1[x],next[x]=next[jump2[x]]+,vis[x]=;
} for(int i=n;i;i--) {
ans+=query(pre[i]);
add(next[i]+,);
}
printf("%lld",ans);
} int main()
{
work();
return ;
}
codeforces459D:Pashmak and Parmida's problem的更多相关文章
- CodeForces 459D Pashmak and Parmida's problem
Pashmak and Parmida's problem Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- 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 ...
- 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); 思路:先从左往右统 ...
- 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, ...
- Pashmak and Parmida's problem(树状数组)
题目链接:http://codeforces.com/contest/459/problem/D 题意: 数列A, ai表示 i-th 的值, f(i,j, x) 表示[i,j]之间x的数目, 问:当 ...
- CF459D Pashmak and Parmida's problem (树状数组)
Codeforces Round #261 (Div. 2) 题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a ...
- 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). 解法:分别从左到右,由右到 ...
- 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预处理出 ...
随机推荐
- 解决EasyNVR现场无固定公网IP的问题,万千企业期待的EasyNVS管理平台是什么?
前言 EasyNVS云管理平台已经研发上线,今天就来给大家讲解一下EasyNVS到底是个啥,有什么特殊的功能呢? 首先,我们先从EasyNVR智能云终端(以前我们常称之为EasyNVR无插件直播服务器 ...
- 在腾讯云服务器上实现java web项目部署
----------------------------博主讲废话 几天前搞了一台体验七天的腾讯云服务器.之前已实现在新浪云下java web项目的部署,不需要自己搭建环境,比较简单,而且自 己也偷懒 ...
- 微信小程序-学习总结(1)
微信小程序尺寸单位 rpx单位是微信小程序中css的尺寸单位,rpx可以根据屏幕宽度进行自适应.规定屏幕宽为750rpx. 设备 rpx换算成px(屏幕宽度/750) ...
- BaseServlet 介绍
1. BaseServlet 的作用 让一个Servlet可以处理多种不同的请求,不同的请求调用Servlet的不同方法. 2. 实现原理 客户端发送请求时, 必须多给出一个参数, 用来说明要调用的方 ...
- MySQL权限系统(二). MySQL提供的特权 Privileges Provided by MySQL
MySQL provides privileges that apply in different contexts and at different levels of operation: Adm ...
- java NIO 模型(一)
1. 阻塞I/O通信模型 1.性能:一连接一线程模型导致服务端的并发接入数和系统吞吐量受到极大限制 2.可靠性:由于IO操作采用同步阻塞模式,当网络拥塞或者逻辑处理缓慢会导致IO线程被挂住,阻塞时间无 ...
- 关于 Content-Type:application/x-www-form-urlencoded 和 Content-Type:multipart/related(转)
转至:http://www.cnblogs.com/taoys/archive/2010/12/30/1922186.html application/x-www-form-urlencoded: 窗 ...
- C语言预处理命令的使用
cppreference.com -> 预处理命令 -> 详细说明 预处理命令 #,## # 和 ## 操作符是和#define宏使用的. 使用# 使在#后的首个参数返回为一个带引号的字符 ...
- ModelForm组件介绍
照抄自:http://www.jb51.net/article/126786.htm ModelForm组件如同它的名字一样就是把model和form结合起来,在有些场景可以起到意想不到的效果. 先来 ...
- 【转】Python爬虫_示例2
爬虫项目:爬取并筛选拉钩网职位信息自动提交简历 一 目标站点分析 #一:实验前准备: 浏览器用Chrome 用Ctrl+Shift+Delete清除浏览器缓存的Cookie 打开network准备 ...