inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
stack<int> S; int a[N], l[N], r[N];
voidclearStack(){ while(!S.empty()) S.pop(); }
inlinevoidwork(int n){ for (int i = 1; i <= n; ++i) { a[i] = rd(); } clearStack(); S.push(1); l[1] = 1; for (int i = 2; i <= n; ++i) { if (a[i] <= a[S.top()]) { while (!S.empty() && a[i] <= a[S.top()]) { l[i] = l[S.top()]; S.pop(); } } else { l[i] = i; } S.push(i); } clearStack(); S.push(n); r[n] = n; for (int i = n - 1; i >= 1; --i) { if (a[i] <= a[S.top()]) { while (!S.empty() && a[i] <= a[S.top()]) { r[i] = r[S.top()]; S.pop(); } } else { r[i] = i; } S.push(i); } ll ans = 0; for (int i = 1; i <= n; ++i) { ans = max(ans, 1ll * a[i] * (r[i] - l[i] + 1)); } printf("%lld\n", ans); }
intmain(){ int n = rd(); while (n) {work(n); n = rd();} return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
stack<int> S; int a[N], l[N];
voidclearStack(){ while(!S.empty()) S.pop(); }
inlinevoidwork(int n){ for (int i = 1; i <= n; ++i) { a[i] = rd(); l[i] = i; } a[++n] = 0; l[n] = n; ll ans = 0; clearStack(); S.push(1); l[1] = 1; for (int i = 2; i <= n; ++i) { while (!S.empty() && a[i] <= a[S.top()]) { ans = max(ans, 1ll * a[S.top()] * (i - l[S.top()])); l[i] = l[S.top()]; S.pop(); } S.push(i); } printf("%lld\n", ans); }
intmain(){ int n = rd(); while (n) {work(n); n = rd();} return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
stack<int> S; int a[N], l[N];
voidclearStack(){ while(!S.empty()) S.pop(); }
inlinevoidwork(int n){ for (int i = 1; i <= n; ++i) { a[i] = rd(); l[i] = i; } ll ans = 0; clearStack(); S.push(1); l[1] = 1; for (int i = 2; i <= n; ++i) { while (!S.empty() && a[i] <= a[S.top()]) { ans = max(ans, 1ll * a[S.top()] * (i - l[S.top()])); l[i] = l[S.top()]; S.pop(); } S.push(i); } while (!S.empty()) { ans = max(ans, 1ll * a[S.top()] * (n - l[S.top()] + 1)); S.pop(); } printf("%lld\n", ans); }
intmain(){ int n = rd(); while (n) {work(n); n = rd();} return0; }
inline ll rd(){ ll x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
stack<int> S; ll a[N]; int f[N];
voidclearStack(){ while(!S.empty()) S.pop(); }
inlinevoidwork(int n){ for (int i = 1; i <= n; ++i) { a[i] = rd(); } clearStack(); S.push(n); for (int i = n - 1; i >= 1; --i) { while (!S.empty() && a[i] >= a[S.top()]) S.pop(); if (!S.empty()) f[i] = S.top(); S.push(i); } for (int i = 1; i <= n; ++i) { printf("%d ", f[i]); } }
inline ll rd(){ ll x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
stack<ll> S; ll a[N];
voidclearStack(){ while(!S.empty()) S.pop(); }
inlinevoidwork(ll n){ for (ll i = 1; i <= n; ++i) { a[i] = rd(); a[i] = rd(); } ll ans = n; clearStack(); S.push(1); for (ll i = 2; i <= n; ++i) { while (!S.empty() && a[i] < a[S.top()]) { S.pop(); } if (!S.empty() && a[S.top()] == a[i]) {ans--; S.pop();} S.push(i); } printf("%lld\n", ans); }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
stack<pair<int, int> > S; int a[N]; ll ans = 0;
voidclearStack(){ while(!S.empty()) S.pop(); }
inlinevoidwork(int n){ for (int i = 1; i <= n; ++i) { a[i] = rd(); } clearStack(); S.push(make_pair(n, 1)); for (int i = n - 1; i >= 1; --i) { int cnt = 1; while (!S.empty() && a[i] >= a[S.top().first]) { ans += S.top().second; if (a[i] == a[S.top().first]) cnt += S.top().second; S.pop(); } ans += (!S.empty()); S.push(make_pair(i, cnt)); } printf("%lld\n", ans); }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
stack<int> S; int f[N][N], l[N], n, m; char a[N][N];
voidclearStack(){ while(!S.empty()) S.pop(); }
inline ll solve(int x){ ll ans = 0; clearStack(); S.push(1); for (int i = 1; i <= m; ++i) l[i] = i; for (int i = 2; i <= m; ++i) { while (!S.empty() && f[x][i] <= f[x][S.top()]) { ans = max(ans, 1ll * f[x][S.top()] * (i - l[S.top()])); l[i] = l[S.top()]; S.pop(); } S.push(i); } while (!S.empty()) { ans = max(ans, 1ll * f[x][S.top()] * (m - l[S.top()] + 1)); S.pop(); } return ans; }
inlinevoidwork(){ n = rd(), m = rd(); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) { for (a[i][j] = getchar(); a[i][j] != 'F' && a[i][j] != 'R'; a[i][j] = getchar()); f[i][j] = (a[i][j] == 'F') ? f[i - 1][j] + 1 : 0; } ll ans = 0; for (int i = 1; i <= n; ++i) {ans = max(ans, solve(i));} printf("%lld\n", 3 * ans); }
intmain(){ work(); return0; }
Post title:单调栈
Post author:Eva.Q
Create time:2022-02-02 22:21:33
Post link:https://qyy/2022/02/02/Algorithm/Monotonic stack/
Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.