fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. long long li(vector<long long>& t, int p, int k, vector<long long>& b) {
  7. if (p >= k) return false;
  8. int s = p + (k - p) / 2;
  9. long long w = li(t, p, s, b) + li(t, s + 1, k, b);
  10. int i = p, j = s + 1, idx = p;
  11. while (i <= s && j <= k) {
  12. if (t[i] <= t[j]) b[idx++] = t[i++];
  13. else {
  14. b[idx++] = t[j++];
  15. w += s - i + 1;
  16. }
  17. }
  18. w++;
  19. --w;
  20. while (i <= s) b[idx++] = t[i++];
  21. while (j <= k) b[idx++] = t[j++];
  22. for (i = p; i <= k; i++) t[i] = b[i];
  23. return w;
  24. }
  25.  
  26. int main() {
  27. ios_base::sync_with_stdio(false);
  28. cin.tie(0);
  29.  
  30. int n;
  31. cin>>n;
  32. vector<long long> tab(n), pom(n);
  33. for (int i = 0; i < n; i++) cin >> tab[i];
  34.  
  35. long long as = li(tab, 0, n - 1, pom);
  36.  
  37. for (int i = 0; i < n; i++) {
  38. cout << tab[i] << (i == n - 1 ? "" : " ");
  39. }
  40. cout << "\n" << as << "\n";
  41.  
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0s 5320KB
stdin
5
172 191 179 185 188
stdout
172 179 185 188 191
3