SGU_180_Inversions
2015-08-17 14:05
357 查看
180. Inversions
time limit per test: 0.25 sec.memory limit per test: 4096 KB
input: standard
output: standard
There are N integers (1<=N<=65537) A1, A2,.. AN (0<=Ai<=10^9). You need to find amount of such pairs (i, j) that 1<=i<j<=N and A[i]>A[j].
Input
The first line of the input contains the number N. The second line contains N numbers A1...AN.
Output
Write amount of such pairs.
Sample test(s)
Input
5 2 3 1 5 4
Output
3
[align=right][submit][/align]
[align=right][forum][/align]
Author: | Stanislav Angelyuk |
Resource: | Saratov ST team Spring Contest #1 |
Date: | 18.05.2003 |
但是有一点这个问题输入并没有保证数字不相同
所以做坐标变换的时候应该在比较值的同时比较一下位置信息
#include <iostream> #include <stdio.h> #include <algorithm> using namespace std; const int M=65540; int tree[M]; struct NODE { int v,p; }node[M]; bool cmp1(NODE a,NODE b) { if(a.v==b.v) return a.p<b.p; //值相同的时候比较下位置信息,这样把相同值离散成不同的小的值得时候 return a.v<b.v; //不会产生同样数值变成的大的数字,位置到前面去影响结果 } bool cmp2(NODE a,NODE b) { return a.p<b.p; } int lowbit(int x) { return x&(-x); } int getsum(int x) { int s=0; for(int i=x;i;i-=lowbit(i)) s+=tree[i]; return s; } void add(int x,int v) { for(int i=x;i<M;i+=lowbit(i)) tree[i]+=v; } int main() { int n; long long co=0; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&node[i].v); node[i].p=i; } sort(node,node+n,cmp1); for(int i=0;i<n;i++) { node[i].v=i+1; } sort(node,node+n,cmp2); for(int i=0;i<n;i++) { co+=i-getsum(node[i].v-1); add(node[i].v,1); } printf("%I64d\n",co); return 0; }
相关文章推荐
- ZOJ-More Divisors
- ADO连接数据库,编译到客户机运行时出现CreateInstance报错0x80004002、CreateParameter报0xC0000005错误的解决方法。
- redis-3.0.3安装测试
- linux 服务器 去掉 codeigniter 路径的index.php
- Myeclipse优化速度
- jquery基础精华04(01)
- android强行隐藏自带的输入法
- Java实现邮件发送
- POJ_1050_ToTheMax
- java 之 工厂模式
- 初识七巧板平台
- linux 下locate命令无法搜索出文件
- jquery基础精华03
- mysql5.6更改数据库编码
- webserver/CGI
- CSS IE Hack
- Mac下Android Studio快捷键
- jquery基础精华02
- 解决ListView的 setEmptyView无效的问题
- Java注释和注释模板