Advertisement
pb_jiang

CF547B WA

Jan 10th, 2023
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #include <assert.h>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define dbg(...) logger(#__VA_ARGS__, __VA_ARGS__)
  5. template <typename... Args> void logger(string vars, Args &&... values)
  6. {
  7.     cerr << vars << " = ";
  8.     string delim = "";
  9.     (..., (cerr << delim << values, delim = ", "));
  10.     cerr << endl;
  11. }
  12.  
  13. template <class T> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m)); }
  14. template <class T> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n)); }
  15. template <class T, T init> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m, init)); }
  16. template <class T, T init> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n, init)); }
  17.  
  18. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  19.  
  20. using ll = long long;
  21. using pii = pair<int, int>;
  22. using vl = vector<ll>;
  23. using vi = vector<int>;
  24.  
  25. int main(int argc, char **argv)
  26. {
  27.     int n;
  28.     cin >> n;
  29.     vi a(n);
  30.     for (auto &x : a)
  31.         cin >> x;
  32.  
  33.     vi sprev(n, -1), snext(n, n);
  34.     stack<int> stck;
  35.     for (int i = 0; i < n; ++i) {
  36.         while (stck.empty() == false && a[stck.top()] >= a[i])
  37.             stck.pop();
  38.         sprev[i] = stck.empty() ? -1 : stck.top();
  39.         stck.push(i);
  40.     }
  41.     while (stck.empty() == false)
  42.         stck.pop();
  43.     for (int i = n - 1; i >= 0; --i) {
  44.         while (stck.empty() == false && a[stck.top()] >= a[i])
  45.             stck.pop();
  46.         sprev[i] = stck.empty() ? n : stck.top();
  47.         stck.push(i);
  48.     }
  49.  
  50.     vector<pii> as(n);
  51.     for (int i = 0; i < n; ++i)
  52.         as[i] = {1e9 - a[i], i};
  53.     sort(as.begin(), as.end());
  54.     vi ans(n);
  55.  
  56.     for (int i = 0; i < n; ++i) {
  57.         auto [val, idx] = as[i];
  58.         val = 1e9 - val;
  59.         int span = snext[idx] - sprev[idx] - 1;
  60.         ans[span - 1] = max(ans[span - 1], val);
  61.     }
  62.  
  63.     int prev = 0;
  64.     for (int i = n - 1; i >= 0; i--)
  65.         prev = max(prev, ans[i]), ans[i] = prev;
  66.     for (auto x : ans)
  67.         cout << x << ' ';
  68.     cout << endl;
  69.     return 0;
  70. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement