Inversions

时间:2021-09-11 10:34:04
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. &lbrack;UCSD白板题&rsqb; 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 &num;301 &lpar;Div&period; 2&rpar; E &period; 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. &lbrack;Swift&rsqb;LeetCode775&period; 全局倒置与局部倒置 &vert; 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. &lbrack;LeetCode&rsqb; 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&period; 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&period; 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. CSS基础知识真难啊

    CSS层叠样式表Cascading Style Sheets CSS派生选择器(上下文选择器): 后代选择器:h1  strong {color:red;}第一个参数和第二个参数之间的代数是可以无限的 ...

  2. kylin1&period;5新特性 new aggregation group

    终于啃完并理解了,我果然弱鸡.new aggregation group,是kylin 1.5的新特性:老版本中的agg是需要选中所有可能被使用的纬度字段,以供查询:但存在高纬度的查询需求,例如查询某 ...

  3. File的保存与读取

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io ...

  4. Error when running Swift3 in REPL

    Traceback (most recent call last): File "", line 1, in NameError: name 'run_one_line' is n ...

  5. 杭电ACM减花布条

    这是原题的地址 http://acm.hdu.edu.cn/showproblem.php?pid=2087 Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条 ...

  6. Thrift安装问题

      1.error: Bison version 2.5 or higher must be installed on the system! 哈哈,Bison版本低了吧,用下面的命令 wget ht ...

  7. php连接到数据库操作

    <?php $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { ?> 要写的内容代码,比如说Ht ...

  8. Android Development HandBook-Android Studio 特别篇

    开发准备中http://www.cnblogs.com/dev2007/p/4059829.html 主要介绍了基础环境的搭建,开发工具主要是Eclipse,由于Android Studio使用越来越 ...

  9. loj2880「JOISC 2014 Day3」稻草人

    题目链接:bzoj4237 ​ loj2880 考虑\(cdq\)分治,按\(x\)坐标排序,于是问题变成统计左下角在\([l,mid]\),右上角在\([mid+1,r]\)的矩形数量 我们先考虑固 ...

  10. Java内存泄漏相关

    之前学习了javaGC的原理机制,有了一定的了解,现在做一个整理总结,便于理解记忆,包括三个问题: 1. java GC是什么时候做的? 2. java GC作用的东西是什么? 3. java GC具 ...