fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <sstream>
  4.  
  5. using namespace std;
  6.  
  7. int FloorLog2(int x)
  8. {
  9. int result = 0;
  10. while (x>>=1)
  11. ++result;
  12. return result;
  13. }
  14.  
  15. bool odd(int x)
  16. {
  17. return x&1==1;
  18. }
  19.  
  20. int inverCount(vector<int> p)
  21. {
  22. int result = 0;
  23. int n = p.size();
  24. int reapat = FloorLog2(n-1)+1;
  25. vector<int> licz(n+1, 0);
  26. for (int j=0; j<reapat; ++j)
  27. {
  28. for (int i=0; i<n; ++i)
  29. {
  30. if (odd(p[i]))
  31. ++licz[p[i]];
  32. else
  33. result += licz[p[i]+1] ;
  34. }
  35. for (int i=0; i<n; ++i)
  36. {
  37. licz[i]=0;
  38. p[i] >>= 1;
  39. }
  40. }
  41. return result;
  42. }
  43.  
  44. int main()
  45. {
  46. string line;
  47. while (getline(cin, line))
  48. {
  49. istringstream data(line);
  50. vector<int> per;
  51. int x;
  52. while (data >>x)
  53. per.push_back(x);
  54. cout << line << " = " << inverCount(per) << endl;
  55. }
  56. return 0;
  57. }
Success #stdin #stdout 0s 3464KB
stdin
0
0 1
1 0
2 1 0
2 0 1
5 4 3 6 0 1 2
stdout
0 = 0
0 1 = 0
1 0 = 1
2 1 0 = 3
2 0 1 = 2
5 4 3 6 0 1 2 = 15