fork download
  1. #include <iostream>
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5.  
  6. int main() {
  7. // your code goes here
  8. int n ; cin>>n;
  9. vector<int>arr(n);
  10. for(int i = 0 ; i< n ; i++) cin>>arr[i];
  11. vector<int>pre(n);
  12. pre[0]=0;
  13. int i = 0 ;
  14. while(i<n){
  15. int c = 0 ; int j = 0 ;
  16. while(j<i){
  17. if(arr[j]<arr[i]) c++;
  18. j++;
  19. }
  20. pre[i]=c; cout<<pre[i]<<" ";
  21. i++;
  22. }
  23. vector<int>suf(n);
  24. suf[n-1]=0;
  25. i = n-2;
  26. while(i>=0){
  27. int j = n-1;int c = 0 ;
  28. while(j>=i){
  29. if(arr[j]>arr[i])c++;
  30. j--;
  31. }
  32. suf[i]=c;
  33. i--;
  34. }
  35. cout<<endl;
  36. i = 0 ;
  37. while(i<n){
  38. cout<<suf[i]<<" ";
  39. i++;
  40. }
  41. //count the number of pairs
  42. int count = 0 ; i = 0 ;
  43. while(i<n){
  44. count += pre[i]*suf[i];
  45. i++;
  46.  
  47. }
  48. cout<<endl<<count;
  49. return 0;
  50. }
Success #stdin #stdout 0s 5312KB
stdin
6
3 5 3 2 7 8
stdout
0 1 0 0 4 5 
3 2 2 2 1 0 6