Codeforces Round #261 (Div. 2)459D. Pashmak and Parmida's problem(求逆序数对)
题目链接:http://codeforces.com/contest/459/problem/D
3 seconds
256 megabytes
standard input
standard output
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 andak = 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
思路:用map预处理出 f(1, i, a[i])
和 f(j,
。再求逆序数对!
n, a[j])
代码例如以下:
#include <cstdio>
#include <map>
using namespace std;
typedef long long LL;
const int Maxn = 1000017;
int n;
int a[Maxn],c[Maxn];
int f[Maxn];
LL res;
map <int, int> freq;
int Lowbit(int x) //2^k
{
return x&(-x);
} void update(int i, int x)//i点增量为x
{
while(i <= n)
{
c[i] += x;
i += Lowbit(i);
}
}
int sum(int x)//区间求和 [1,x]
{
int sum=0;
while(x>0)
{
sum+=c[x];
x-=Lowbit(x);
}
return sum;
}
int main()
{
while(~scanf("%d", &n))
{
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
for (int i = n - 1; i >= 0; i--)
f[i] = ++freq[a[i]];
freq.clear();
//树状数组求逆序
for (int i = 0; i < n; i++)
{
//res += sum(f[i] + 1);
res += i - sum(f[i]);//逆序数个数
update(++freq[a[i]],1);
}
printf("%I64d\n", res);
}
return 0;
}
贴一个解说比較具体的链接:http://www.cnblogs.com/bfshm/p/3916799.html
Codeforces Round #261 (Div. 2)459D. Pashmak and Parmida's problem(求逆序数对)的更多相关文章
- 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预处理出 ...
- 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, ...
- Codeforces Round #261 (Div. 2) B. Pashmak and Flowers 水题
题目链接:http://codeforces.com/problemset/problem/459/B 题意: 给出n支花,每支花都有一个漂亮值.挑选最大和最小漂亮值得两支花,问他们的差值为多少,并且 ...
- Codeforces Round #261 (Div. 2)459A. Pashmak and Garden(数学题)
题目链接:http://codeforces.com/problemset/problem/459/A A. Pashmak and Garden time limit per test 1 seco ...
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP
http://codeforces.com/contest/459/problem/E 不明确的是我的代码为啥AC不了,我的是记录we[i]以i为结尾的点的最大权值得边,然后wa在第35 36组数据 ...
- Codeforces Round 261 Div.2 E Pashmak and Graph --DAG上的DP
题意:n个点,m条边,每条边有一个权值,找一条边数最多的边权严格递增的路径,输出路径长度. 解法:先将边权从小到大排序,然后从大到小遍历,dp[u]表示从u出发能够构成的严格递增路径的最大长度. dp ...
- Codeforces Round #261 (Div. 2)[ABCDE]
Codeforces Round #261 (Div. 2)[ABCDE] ACM 题目地址:Codeforces Round #261 (Div. 2) A - Pashmak and Garden ...
- Codeforces Round #261 (Div. 2) E
Description Pashmak's homework is a problem about graphs. Although he always tries to do his homewor ...
随机推荐
- 串口接收模块(verilog) 波特率115200
我来分享一下uart协议之接收verilog代码 顶层实例化 `timecale 1ns / 1ps////////////////////////////////////////////////// ...
- Robotframework自动化系统:筛选结果数量统计
Robotframework自动化系统:筛选结果数量统计 上一个节点已经可以随机选中某一个下拉框的值,我们在使用evaluate随机数的时候需要计算下拉选项总数,这时候我们是手工计算输入的:这时候如果 ...
- sublime 集成git插件,及git常用命令
以前用了很久的totoiseSVN,显然不够高大上,开发界的版本装x利器还得是git.然而git的命令行对没怎么用过linux的人可能觉得还不如用gitGUI,可是当你知道sublime可以集成git ...
- KVM管理平台openebula安装
1.1opennebula控制台的安装 (如果要添加映像需要给200G以上给/var/lib/one,本文是共享/var/lib/one实现监控,用映像出创建虚拟机原理是从opennebula控制平台 ...
- Node.js初探之GET方式传输
Node.js初探之GET方式传输 例子:form用GET方法向后台传东西 html文件: <form action="http://localhost:8080/aaa" ...
- 开篇-我眼中的FPGA
既然是开篇,那就来闲话叨一叨FPGA吧. 掰掰指头,结识FPGA估计有5年多.作为嵌入式工程师,每天的日常充斥着ARM.DSP.操作系统.通讯.总线等耳熟能详的词汇,徜徉其中不能自拔,而这其中,自觉最 ...
- js如何判断对象和数组
var a = {}; var b = []; console.log(Object.prototype.toString.call(a) === '[object Object]');//判断对象 ...
- pandas基本介绍-【老鱼学pandas】
前面我们学习了numpy,现在我们来学习一下pandas. Python Data Analysis Library 或 pandas 主要用于处理类似excel一样的数据格式,其中有表头.数据序列号 ...
- concurrent.futures
concurrent.futures concurrent.futures提供高层次的接口,用来实现异步调用. 这个异步执行可以使用threads(ThreadPoolExecutor)或者proce ...
- 2017EIS CTFwriteup
EIS2017也就是2017年高校网络信息安全管理 运维挑战赛,全国一百多所高校参赛,侥幸拿了个地区三等奖,事先不知道理论赛占分比,不然就会是二等奖(吐槽),生活没有如果,下次努力吧. 比赛已经结束大 ...