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. NLP初步

    [NLP初步] NLP是Natural Lanuage Process的缩写.搜索引擎可以通过关词匹配和完成很多的任务, 比如话题搜索(搜索包含律师, 法院, 控告等词的文档), 但是搜索引擎无法理解 ...

  2. keil编译时出现*** WARNING L2: REFERENCE MADE TO UNRESOLVED EXTERNAL

    *** WARNING L2: REFERENCE MADE TO UNRESOLVED EXTERNAL *** WARNING L1: UNRESOLVED EXTERNAL SYMBOL 解决: ...

  3. tryparse的用法,^0*[1-9]\d*$

    Entry entry = new Entry(); Int32 iParam; if(Int32.TryParse(entry.ajh,out iParam)) { /*如果转换成功就输出iPara ...

  4. nginx打开目录游览功能

    #开启索引功能 location / { autoindex on; autoindex_exact_size off; autoindex_localtime on; } #别名目录location ...

  5. jquery 应用小结

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. preventDefault stopPropagation??

    棒棒哒~ event.preventDefault https://developer.mozilla.org/zh-CN/docs/Web/API/Event/preventDefault stop ...

  7. App Submission Issues

    查看原文: http://leancodingnow.com/app-submission-issues/ I bet many iOS developers are busy submitting ...

  8. Linux系统编程——进程间通信:命名管道(FIFO)

    命名管道的概述 无名管道,因为没有名字,仅仅能用于亲缘关系的进程间通信(很多其它详情.请看<无名管道>).为了克服这个缺点.提出了命名管道(FIFO).也叫有名管道.FIFO 文件. 命名 ...

  9. Java带包编译运行

    package cn.togeek.job; public class Test { public static void main(String[] args) throws Exception { ...

  10. mysql主从同步单个表实验记录

    问题的提出: 在CRM管理系统与运营基础数据平台之间需要有数据表进行交换,说是交换,其实是单向的,就是CRM里面的一些数据需要实时同步到运营基础数据平台中. 解决方案: A.采用时间戳的办法进行代码开 ...