Advertisement
pb_jiang

CF547B AC

Jan 11th, 2023
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 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.         snext[i] = stck.empty() ? n : stck.top();
  47.         stck.push(i);
  48.     }
  49.     vi ans(n);
  50.  
  51.     /*
  52.     dbg(n);
  53.     for (int i = 0; i < n; ++i) {
  54.         int x = a[i], sp = sprev[i], sn = snext[i];
  55.         dbg(x, sp, sn);
  56.     }
  57.     vector<pii> as(n);
  58.     for (int i = 0; i < n; ++i)
  59.         as[i] = {1e9 - a[i], i};
  60.     sort(as.begin(), as.end());
  61.  
  62.     for (int i = 0; i < n; ++i) {
  63.         auto [val, idx] = as[i];
  64.         val = 1e9 - val;
  65.         int span = snext[idx] - sprev[idx] - 1;
  66.         ans[span - 1] = max(ans[span - 1], val);
  67.     }
  68.     */
  69.     for (int i = 0; i < n; ++i) {
  70.         int span = snext[i] - sprev[i] - 1;
  71.         ans[span - 1] = max(ans[span - 1], a[i]);
  72.     }
  73.  
  74.     int prev = 0;
  75.     for (int i = n - 1; i >= 0; i--)
  76.         prev = max(prev, ans[i]), ans[i] = prev;
  77.     for (auto x : ans)
  78.         cout << x << ' ';
  79.     cout << endl;
  80.     return 0;
  81. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement