fork download
  1. #include<iostream>
  2. #include<vector>
  3. #include<algorithm>
  4. #define SZ(X) ((int)(X).size())
  5. using namespace std;
  6. long long dc(vector<int> d) {
  7. int mi = *min_element(d.begin(), d.end());
  8. int ma = *max_element(d.begin(), d.end());
  9. if(mi == ma) return 0;
  10. int mid = mi + (ma - mi) / 2;
  11. vector<int> a, b;
  12. int now = 0;
  13. long long ret = 0;
  14. for(int i = 0; i < SZ(d); i++) {
  15. if(d[i] <= mid) {
  16. a.push_back(d[i]);
  17. ret += SZ(b);
  18. } else b.push_back(d[i]);
  19. }
  20. return ret + dc(a) + dc(b);
  21. }
  22. int main() {
  23. int n;
  24. cin >> n;
  25. vector<int> d(n);
  26. for(int &x: d) cin >> x;
  27. cout << dc(d) << '\n';
  28. }
Success #stdin #stdout 0.01s 5292KB
stdin
5
3 -4 3 -1 2
stdout
5