fork download
  1. #include <stdio.h>
  2. int s[100000],ct,sum;
  3. void count(int val);
  4. void mergesort(int a[],int l,int h);
  5. int main(void) {
  6. // your code goes here
  7. int n,i;
  8. scanf("%d",&n);
  9. int a[n];
  10. for(i=0;i<n;i++)
  11. scanf("%d",a+i);
  12. mergesort(a,0,n-1);
  13. printf("%d",sum);
  14. return 0;
  15. }
  16. void mergesort(int a[],int l,int h) // O(nlogn)
  17. {
  18. if(l<h)
  19. {
  20. int mid=l+(h-l)/2;
  21. mergesort(a,l,mid);
  22. mergesort(a,mid+1,h);
  23. }
  24. else if(l==h)
  25. {
  26. count(a[l]);
  27. }
  28. }
  29. void count(int val) //O(n)
  30. {
  31. int i;
  32. if(ct==0)
  33. {
  34. s[0]=val;
  35. ct++;
  36. }
  37. else
  38. {
  39. for(i=0;i<ct;i++)
  40. {
  41. if(s[i]>val)
  42. {
  43. sum+=1;
  44. // printf("pair = %d %d\n",s[i],val);
  45. }
  46. }
  47. s[i]=val;
  48. ct+=1;
  49. }
  50. }
  51.  
Success #stdin #stdout 0.03s 4544KB
stdin
Standard input is empty
stdout
627842