Advertisement
Kevin_Zhang

Untitled

Sep 26th, 2020
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define pb emplace_back
  3. using namespace std;
  4. using ll = long long;
  5. #ifdef KEV
  6. #define DE(i, e) cerr << #i << ' ' << i << e
  7. void debug(auto L, auto R) { while (L < R) cerr << *L << " \n"[L+1==R], ++L; }
  8. #else
  9. #define DE(...) 0
  10. void debug(...) {}
  11. #endif
  12. const int maxn = 1000010;
  13. int n, pf[maxn], v[maxn], dpl[maxn], dpr[maxn], sl[maxn], sr[maxn];
  14. char w[maxn];
  15. int solve(int *dp) {
  16.     static int cnt;
  17.     ++cnt;
  18.     for (int i = 1;i <= n;++i)
  19.         pf[i] = pf[i-1] + v[i];
  20.     stack<int> st;
  21.     st.push(0);
  22.     int res = 0;
  23. #define DP(i) (cnt==1?dp[i] : dp[n-i+1])
  24.     for (int i = 1;i <= n;++i) {
  25.         while (st.size() &&
  26.                 pf[st.top()] > pf[i])
  27.             st.pop();
  28.        
  29.         DP(i) = st.empty() ? 0 : DP(st.top()) + i - st.top();
  30.         st.push(i);
  31.     }
  32.     return res;
  33. }
  34. signed main(){
  35.     ios_base::sync_with_stdio(0), cin.tie(0);
  36.     cin >> n >> w+1;
  37.     for (int i = 1;i <= n;++i)
  38.         v[i] = w[i] != 'j' ? 1 : -1;
  39.     solve(dpl);
  40.     reverse(v+1, v+1+n);
  41.     solve(dpr);
  42.     for (int i = 1;i <= n;++i)
  43.         sr[i] = max(sr[i-1], dpl[i] + i - 1);
  44.     sl[n+1] = n+1;
  45.     for (int i = n;i >= 1;--i)
  46.         sl[i] = min(sl[i+1], i - dpr[i] + 1);
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement