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 ...
随机推荐
- Linux中内存查看命令free详解(转)
add by zhj:说了那么多,其实看第一行就足够了,free项就是未使用的内存.其实,我是感觉压根就没必要 使用free命令,用top代替就行了 原文:http://liustb.blog.163 ...
- 删除Ngnix 日志
删除Ngnix日志的脚本 #!/bin/bash #初始化 LOGS_PATH=$(pwd)/logs YESTERDAY=$(date -d "yesterday" +%Y-%m ...
- Physicals
[Physicals] The physics simulation in Sprite Kit is performed by adding physics bodies to scenes. [T ...
- LC并联谐振回路
- POJ3345
http://poj.org/problem?id=3345 大意: 大意是说现在有n个城市来给你投票,你需要至少拿到m个城市的赞成票.想要获得第i个城市的赞成需要花费w[i],有个条件就是某些城市是 ...
- POJ 3617 Best Cow Line (贪心)
题意:给定一行字符串,让你把它变成字典序最短,方法只有两种,要么从头部拿一个字符,要么从尾部拿一个. 析:贪心,从两边拿时,哪个小先拿哪个,如果一样,接着往下比较,要么比到字符不一样,要么比完,也就是 ...
- C#取得当前目录 转载
/获取包含清单的已加载文件的路径或 UNC 位置. public static string sApplicationPath = Assembly.GetExecutingAssem ...
- Python之函数进阶
本节内容 上一篇中介绍了Python中函数的定义.函数的调用.函数的参数以及变量的作用域等内容,现在来说下函数的一些高级特性: 递归函数 嵌套函数与闭包 匿名函数 高阶函数 内置函数 总结 一.递归函 ...
- plsql配置连接远程数据库
一.首先安装PL/SQL Developer 下载地址:https://yunpan.cn/cM3njKpfK8MnT 访问密码 996a 二.再安装instantclient_11_2 下载地址:h ...
- Xcode 6 模拟器路径
原文地址:http://leancodingnow.com/xcode-6-simulator-folder/ 本文主要介绍一下Xcode 6的iOS模拟器的应用目录的变化. Xcode 5的iOS模 ...