Advertisement
Kevin_Zhang

Untitled

Dec 15th, 2020
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define pb emplace_back
  3. #define AI(i) begin(i), end(i)
  4. using namespace std;
  5. using ll = long long;
  6. template<class T>
  7. bool chmax(T &val, T nv) { return val < nv ? (val = nv, true) : false; }
  8. template<class T>
  9. bool chmin(T &val, T nv) { return nv < val ? (val = nv, true) : false; }
  10. #ifdef KEV
  11. #define DE(args...) kout("[ " + string(#args) + " ] = ", args)
  12. void kout() {cerr << endl;}
  13. template<class T1, class ...T2>
  14. void kout (T1 v, T2 ...e) { cerr << v << ' ', kout(e...); }
  15. template<class T>
  16. void debug(T L, T R) { while (L != R) cerr << *L << " \n"[next(L)==R], ++L; }
  17. #else
  18. #define DE(...) 0
  19. #define debug(...) 0
  20. #endif
  21. // What I should check
  22. // 1. overflow
  23. // 2. corner cases
  24. // Enjoy the problem instead of hurrying to AC
  25. // Good luck !
  26. /*
  27. ID: ckevin31
  28. LANG: C++14
  29. TASK:
  30. */
  31. #ifndef KEV
  32. inline void IO(string name) {
  33.     freopen((name + ".in").c_str(), "r", stdin);
  34.     freopen((name + ".out").c_str(), "w", stdout);
  35. }
  36. #else
  37. inline void IO(string name){}
  38. #endif
  39. const int MAX_N = 300010;
  40. int a[MAX_N], n;
  41. ll solve(int l, int r) {
  42.     ll res = 0;
  43.     for (int i = l;i <= r;++i) {
  44.         for (int j = r;j >= i;--j) if (a[j] < a[i]) {
  45.             res += j - i + 1;
  46.             break;
  47.         }
  48.     }
  49.     return res;
  50. }
  51. ll quickish_sort(int l, int r) {
  52.     ll res = r - l + 1;
  53.  
  54.     for (int i = l;i < r;++i) {
  55.         if (a[i] > a[i+1]) swap(a[i], a[i+1]);
  56.     }
  57.  
  58.     vector<int> pfmx(n + 1), sufmn(n + 1);
  59.     pfmx[l] = a[l];
  60.     for (int i = l + 1;i <= r;++i)
  61.         pfmx[i] = max(pfmx[i-1], a[i]);
  62.     sufmn[r] = a[r];
  63.     for (int i = r-1;i >= l;--i)
  64.         sufmn[i] = min(sufmn[i+1], a[i]);
  65.  
  66.     int L = l;
  67.     for (int i = l;i < r;++i)
  68.         if (pfmx[i] <= sufmn[i+1])
  69.             res += solve(L, i), L = i+1;
  70.     return res;
  71.  
  72. }
  73. int32_t main() {
  74.     ios_base::sync_with_stdio(0), cin.tie(0);
  75.     IO("sort");
  76.     cin >> n;
  77.     for (int i = 1;i <= n;++i)
  78.         cin >> a[i];
  79.     cout << quickish_sort(1, n) << '\n';
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement