Advertisement
Rimifawfaw

toph count the chaos

Feb 1st, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include<bits/stdc++.h>/*rimi*/
  2. using namespace std;
  3. #define max 100005
  4. int ar[max];
  5. vector<int >tree(max*4);
  6. int k=1;
  7. pair<int, int> t[4*100];
  8. int n;
  9. int temp[1000001];
  10. long long match(int ar[],int left ,int mid, int right)
  11. {
  12. int count=0;
  13. int i,j,k,l,m,o,p;
  14. i = left;
  15. j = mid;
  16. k=left;
  17.  
  18. while((i<mid) &&(j<=right))
  19. {
  20. if(ar[i]>ar[j])
  21. {
  22. temp[k++] = ar[j++];
  23. count += mid-i;
  24. }
  25. else
  26. {
  27. temp[k++] = ar[i++];
  28. }
  29. }
  30. while(i<mid)
  31. {
  32. temp[k++]=ar[i++];
  33. }
  34. while(j<=right)
  35. {
  36. temp[k++] = ar[j++];
  37. }
  38. for(i=left;i<=right;i++)
  39. {
  40. ar[i]=temp[i];
  41. }
  42. return count;
  43.  
  44. }
  45.  
  46. int partition(int ar[],int left, int right)
  47. {
  48. int count =0;
  49. if(left<right)
  50. {
  51. int mid = (left+right)/2;
  52. count+= partition(ar,left,mid);
  53. count+= partition(ar,mid+1,right);
  54. count+= match(ar,left,mid+1,right);
  55. }
  56. return count;
  57. }
  58.  
  59. int main()
  60. {
  61.  
  62. ios_base::sync_with_stdio(false);
  63. cin.tie(NULL);
  64.  
  65. long long int z,m,a,b,c,t,i,j,n,k,h,e,w,q,l,o,d;
  66. double dou;
  67. string s1,s2,str;
  68. map<string, int>ma;
  69. map<string, int>::iterator it;
  70. cin>>n;
  71. int ar[n];
  72. //int temp[n];
  73. for(i=0;i<n;i++)
  74. {
  75. cin>>ar[i];
  76. }
  77. cout<<partition(ar,0,n)<<endl;
  78. return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement