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预处理出 ...
随机推荐
- golang 模板(template)的常用基本语法
1. 模板 在写动态页面的网站的时候,我们常常将不变的部分提出成为模板,可变部分通过后端程序的渲染来生成动态网页,golang提供了html/template包来支持模板渲染. 这篇文章不讨论gola ...
- 巨蟒django之CRM3 添加和编辑客户&&公户和私户的展示和转换
昨日内容回顾: day66 1. 内容回顾 1. 数据的展示 数据通过ORM查询出来 对象列表 QuerySet 1. 普通的字段 对象.字段名 ——> 数据库中的值 2. choices (( ...
- mysql_表_操作
1.创建表 # 基本语法: create table 表名( 列名 类型 是否可以为空 默认值 自增 主键, 列名 类型 是否可以为空 )ENGINE=InnoDB DEFAULT CHARSET=u ...
- Introspection in Python How to spy on your Python objects Guide to Python introspection
Guide to Python introspection https://www.ibm.com/developerworks/library/l-pyint/ Guide to Python in ...
- extract
w http://php.net/manual/en/function.extract.php <?php /* Suppose that $var_array is an array retu ...
- 利用jdk中工具完成Java程序监控方法记录
转载加自己整理的部分内容,转载自:http://jiajun.iteye.com/blog/810150 记录下JConsole使用方法 一.JConsole是什么 从Java 5开始 引入了 ...
- ajax异步请求分页显示
html代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- 基于Cpython的 GIL(Global Interpreter Lock)
一 介绍 定义: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native t ...
- LINUX 文件/组/帮助/权限/文件压缩/管道
Linux文件/目录详解 常用文件路径的作用 /var/log/messages 系统类的日志文件 /var/log/secure 登录日志文件 /var/spool/cron 定时任务目录 /etc ...
- Oracle11g用户频繁锁定并且解锁后不允许登录
原因有可能是oracle的密码过期机制导致的:一.由于Oracle中默认在default概要文件中设置了“PASSWORD_LIFE_TIME=180天”所导致.解决办法:1.查看用户用的哪种prof ...