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 ...
随机推荐
- eclipse构建maven+scala+spark工程
前提条件 下载安装Scala IDE build of Eclipse SDK 构建工程 1.新建maven工程 2.配置项目信息 3.新建scala对应的Source Folder 4.添加scal ...
- openpose模型在AI challenge人体骨骼关键点检测的表现
因为之前正好看了CMU在CVPR2017上的论文<Realtime Multi-Person 2D Pose Estimation using Part Affinity Fields>, ...
- Java实现Html转PDF的方法
写在前面 以下路径问题根据项目结构自己修改,以下是我使用spring boot打成jar包的写法. 一.需求背景 在前端编辑器中输入任意的文本,包括css样式变化,保存为html文本.通过Java后台 ...
- 认识 Less
CSS(层叠样式表)是一项出色的技术,它使得网页的表现与内容完全分离,使网站维护工作变得更容易,不会因为内容的改变而影响表现,也不会因为表现的改变而影响内容. 作为一门标记性语言,CSS 的先天性优点 ...
- 今天用node的cheerio模块做了个某乎的爬虫
一时兴起,想做个爬虫,经过各种深思熟虑,最后选择了某乎,毕竟现在某乎的数据质量还是挺高的.说干就干 打开某乎首页,随便搜索了一串关键字,相关的问题和答案就展现在眼前,我就思考怎么把这些搜索结果 ...
- 快速上手使用Maven
maven的相关命令 mvn archetype:create :创建 Maven 项目 mvn compile :编译源代码(编译到target文件夹中) mvn test-compile :编译测 ...
- 使用echarts,制作色温图
1.需要下载echarts的echarts-all.js文件和创建地图需要用到的数据源 2.在项目中创建jsp文件,将js文件引入 <script type="text/javascr ...
- linux禁用锁定和解除解锁用户账号的方法
Linux系统使用的是/etc/shadow保存加密了的用户密码,要禁止一个帐号的话,最快的方法就是修改存储于/etc/shadow中的密码. 一般情况下,一个有效的Linux用户在/etc/shad ...
- php RAS加密类代码
通过openssl实现的签名.验签.非对称加解密,需要配合x.509证书(如crt和pem)文件使用. <?php /** * RSA算法类 * 签名及密文编码:base64字符串/十六进制字符 ...
- yii2数据条件查询-where专题
条件查询 $customers = Customer::find()->where($cond)->all(); $cond就是我们所谓的条件,条件的写法也根据查询数据的不同存在差异,那么 ...