Inversions
Input
Output
Sample Input
2 3 1 5 4
- #include"iostream"
- #include"algorithm"
- #include"cstring"
- #include"cstdio"
- using namespace std;
- #define max 65550
- structab
- {
- int value;
- int index;
- }a[max];
- /*bool cmp(const ab &a,const ab &b)
- {
- if(a.value!=b.value)
- return a.value<b.value;
- else
- return a.index<b.index;
- }*/
- bool cmp(const ab&a,const ab&b)
- {
- return a.value<b.value;
- }
- int c[max],b[max];
- int n;
- int lowbit(int x)
- {
- return x&(-x);
- }
- void updata(int x,int d)
- {
- while(x<=n)
- {
- c[x]+=d;
- x+=lowbit(x);
- }
- }
- int getsum(int x)
- {
- int res=0;
- while(x>0)
- {
- res+=c[x];
- x-=lowbit(x);
- }
- return res;
- }
- int main()
- {
- int i;
- scanf("%d",&n);
- for(i=1;i<=n;i++)
- {
- scanf("%d",&a[i].value);
- a[i].index=i;
- }
- sort(a+1,a+1+n,cmp);
- b[a[1].index]=1;
- memset(c,0,sizeof(c));
- //memset(b,0,sizeof(b));
- for(i=2;i<=n;i++)
- {
- if(a[i].value!=a[i-1].value)
- b[a[i].index]=i;
- else
- b[a[i].index]=b[a[i-1].index];
- }
- long long int sum=0;
- for(i=1;i<=n;i++)
- {
- updata(b[i],1);
- sum+=getsum(n)-getsum(b[i]);
- }
- printf("%lld\n",sum);
- return 0;
- }
Inversions的更多相关文章
- [UCSD白板题] Number of Inversions
Problem Introduction An inversion of a sequence \(a_0,a_1,\cdots,a_{n-1}\) is a pair of indices \(0 ...
- Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数
E. Infinite Inversions ...
- Inversions After Shuffle
Inversions After Shuffle time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 《算法导论》Problem 2-4 Inversions
在Merge Sort的基础上改改就好了. public class Inversions { public static int inversions(int [] A,int p, int r) ...
- Dynamic Inversions II 逆序数的性质 树状数组求逆序数
Dynamic Inversions II Time Limit: 6000/3000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Other ...
- Dynamic Inversions 50个树状数组
Dynamic Inversions Time Limit: 30000/15000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others ...
- [Swift]LeetCode775. 全局倒置与局部倒置 | Global and Local Inversions
We have some permutation Aof [0, 1, ..., N - 1], where N is the length of A. The number of (global) ...
- [LeetCode] Global and Local Inversions 全局与局部的倒置
We have some permutation A of [0, 1, ..., N - 1], where N is the length of A. The number of (global) ...
- 775. Global and Local Inversions
We have some permutation A of [0, 1, ..., N - 1], where N is the length of A. The number of (global) ...
- 775. Global and Local Inversions局部取反和全局取反
[抄题]: We have some permutation A of [0, 1, ..., N - 1], where N is the length of A. The number of (g ...
随机推荐
- InterfaceConnect
GUID aguid; _di_IInterface a, c; Calld::TEventSink* FEventSink; Server_tlb::_di_IServerWithEvents FS ...
- Python 代码性能优化技巧(转)
原文:Python 代码性能优化技巧 Python 代码优化常见技巧 代码优化能够让程序运行更快,它是在不改变程序运行结果的情况下使得程序的运行效率更高,根据 80/20 原则,实现程序的重构.优化. ...
- HDU 2035 人见人爱A^B 分类: ACM 2015-06-22 23:54 9人阅读 评论(0) 收藏
人见人爱A^B Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- [原创]Devexpress XtraReports 系列 3 创建主从报表
昨天写了系列的第二篇Devexpress XtraReports 系列 2 创建表格报表 . 今天我们来继续系列 3 创建主从报表 首先我们来看看最后实现的效果.Demo最后附上. 开始吧. 第一步, ...
- linux极点五笔无法输入词组_ibus设置
菜鸟学linux——用的是ubuntu 不知道是不是按个哪些快捷键,极点五笔突然无法输入词组.那个抓狂啊 没关系,设置一下就ok 第一步:右上角输入法,右键——>首选项——>常规——> ...
- POJ3468 A Simple Problem with Integers(线段树延时标记)
题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...
- CodeForces 702B Powers of Two (暴力,优化)
题意:给定 n 个数,问你从有多少下标 i < j,并且 ai + aj 是2的倍数. 析:方法一: 从输入开始暴力,因为 i < j 和 i > j 是一样,所以可以从前面就开始查 ...
- Classifier4J的中文支持
Classifier4J是一个轻量级的分类工具,支持贝叶斯分类.向量空间模型.信息摘要等.然而它却不支持中文,异常信息大致如下: Exception in thread "main" ...
- fastcgi 分布式
以lighttpd fastcgi写一下自己对fastcgi分布式的理解. 假设一台机器A上运行lighttpd,在这台主机上只是对请求进行分发. 而在其他多台机器上运行多个fastcgi进程,用来接 ...
- ASP.NET MVC- ActionFilter的使用
ActionFilter是穿插在Action执行过程,在Action执行前后提供扩展的功能.ActionFilter用途非常的广,用在页面压缩.缓存.错误处理,登陆验证. ActionFilter的实 ...