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 Input

Sample test(s)
Input
 
 
5
2 3 1 5 4
 
 
Output
 
 
3
  1. #include"iostream"
  2. #include"algorithm"
  3. #include"cstring"
  4. #include"cstdio"
  5. using namespace std;
  6. #define max 65550
  7. structab
  8. {
  9. int value;
  10. int index;
  11. }a[max];
  12. /*bool cmp(const ab &a,const ab &b)
  13. {
  14. if(a.value!=b.value)
  15. return a.value<b.value;
  16. else
  17. return a.index<b.index;
  18. }*/
  19. bool cmp(const ab&a,const ab&b)
  20. {
  21. return a.value<b.value;
  22. }
  23. int c[max],b[max];
  24. int n;
  25. int lowbit(int x)
  26. {
  27. return x&(-x);
  28. }
  29. void updata(int x,int d)
  30. {
  31. while(x<=n)
  32. {
  33. c[x]+=d;
  34. x+=lowbit(x);
  35. }
  36. }
  37. int getsum(int x)
  38. {
  39. int res=0;
  40. while(x>0)
  41. {
  42. res+=c[x];
  43. x-=lowbit(x);
  44. }
  45. return res;
  46. }
  47. int main()
  48. {
  49. int i;
  50. scanf("%d",&n);
  51. for(i=1;i<=n;i++)
  52. {
  53. scanf("%d",&a[i].value);
  54. a[i].index=i;
  55. }
  56. sort(a+1,a+1+n,cmp);
  57. b[a[1].index]=1;
  58. memset(c,0,sizeof(c));
  59. //memset(b,0,sizeof(b));
  60. for(i=2;i<=n;i++)
  61. {
  62. if(a[i].value!=a[i-1].value)
  63. b[a[i].index]=i;
  64. else
  65. b[a[i].index]=b[a[i-1].index];
  66. }
  67. long long int sum=0;
  68. for(i=1;i<=n;i++)
  69. {
  70. updata(b[i],1);
  71. sum+=getsum(n)-getsum(b[i]);
  72. }
  73. printf("%lld\n",sum);
  74. return 0;
  75. }

Inversions的更多相关文章

  1. [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 ...

  2. Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数

                                                                    E. Infinite Inversions               ...

  3. Inversions After Shuffle

    Inversions After Shuffle time limit per test 1 second memory limit per test 256 megabytes input stan ...

  4. 《算法导论》Problem 2-4 Inversions

    在Merge Sort的基础上改改就好了. public class Inversions { public static int inversions(int [] A,int p, int r) ...

  5. Dynamic Inversions II 逆序数的性质 树状数组求逆序数

    Dynamic Inversions II Time Limit: 6000/3000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Other ...

  6. Dynamic Inversions 50个树状数组

    Dynamic Inversions Time Limit: 30000/15000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others ...

  7. [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) ...

  8. [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) ...

  9. 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) ...

  10. 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 ...

随机推荐

  1. InterfaceConnect

    GUID aguid; _di_IInterface a, c; Calld::TEventSink* FEventSink; Server_tlb::_di_IServerWithEvents FS ...

  2. Python 代码性能优化技巧(转)

    原文:Python 代码性能优化技巧 Python 代码优化常见技巧 代码优化能够让程序运行更快,它是在不改变程序运行结果的情况下使得程序的运行效率更高,根据 80/20 原则,实现程序的重构.优化. ...

  3. 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 ...

  4. [原创]Devexpress XtraReports 系列 3 创建主从报表

    昨天写了系列的第二篇Devexpress XtraReports 系列 2 创建表格报表 . 今天我们来继续系列 3 创建主从报表 首先我们来看看最后实现的效果.Demo最后附上. 开始吧. 第一步, ...

  5. linux极点五笔无法输入词组_ibus设置

    菜鸟学linux——用的是ubuntu 不知道是不是按个哪些快捷键,极点五笔突然无法输入词组.那个抓狂啊 没关系,设置一下就ok 第一步:右上角输入法,右键——>首选项——>常规——> ...

  6. POJ3468 A Simple Problem with Integers(线段树延时标记)

    题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...

  7. CodeForces 702B Powers of Two (暴力,优化)

    题意:给定 n 个数,问你从有多少下标 i < j,并且 ai + aj 是2的倍数. 析:方法一: 从输入开始暴力,因为 i < j 和 i > j 是一样,所以可以从前面就开始查 ...

  8. Classifier4J的中文支持

    Classifier4J是一个轻量级的分类工具,支持贝叶斯分类.向量空间模型.信息摘要等.然而它却不支持中文,异常信息大致如下: Exception in thread "main" ...

  9. fastcgi 分布式

    以lighttpd fastcgi写一下自己对fastcgi分布式的理解. 假设一台机器A上运行lighttpd,在这台主机上只是对请求进行分发. 而在其他多台机器上运行多个fastcgi进程,用来接 ...

  10. ASP.NET MVC- ActionFilter的使用

    ActionFilter是穿插在Action执行过程,在Action执行前后提供扩展的功能.ActionFilter用途非常的广,用在页面压缩.缓存.错误处理,登陆验证. ActionFilter的实 ...