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 ...
随机推荐
- HTML学习笔记 cs2D3D展示基础 第十四节 (原创) 参考使用表
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【吐槽】关于256个 class可以覆盖一个id的问题
还是说今天下午面试的事情,被面试官问了 40多分钟的问题,我觉得丫 一定是从哪个网站down了几份面试题,自个儿整合了一下,然后挨个问,刚开始感觉哟,不错哦,面试官懂的蛮多的. 然后问到某个问题之后, ...
- 关于ThinkPHP中的时间自动填充
<?php class NewsModel extends Model{ protected $_auto = array( array('time_at','mydate','1','call ...
- Cordova使用build命令出错: Could not create the Java Virtual Machine.
这是因为虚拟机内存不足导致的,解决方案如下: _JAVA_OPTIONS=-Xmx512M
- Three ways to throw exception in C#. Which is your preference?
There are three ways to 'throw' a exception in C# C#中有三种抛出异常的方式 Use the throw keyword without an id ...
- 三种读取HashMap的方式
package com.biubiu.entity; import java.util.Collection; import java.util.HashMap; import java.util.I ...
- javascript第四章--面向对象的程序设计
① 理解对象 ② 创建对象 ③ 继承
- Failed to call ScheduleShutdown in logind, proceeding with immediate shutdown: Access denied
装了fedora23后定时关机:sudo shutdown -h +60 弹出如下信息后立刻就关机了: Failed to call ScheduleShutdown in logind, proce ...
- angular指令的简单练习
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Java URL类踩坑指南
背景介绍 最近再做一个RSS阅读工具给自己用,其中一个环节是从服务器端获取一个包含了RSS源列表的json文件,再根据这个json文件下载.解析RSS内容.核心代码如下: class Presente ...