版本比较
单调栈
正文
增加 47 行删除 47 行
19 行未改动20212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666713 行未改动818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271285 行未改动13413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616715 行未改动18318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823919 行未改动int main(){cin >> n;for(int i = 1; i <= n; i++){cin >> a[i] >> w[i];}for(int i = 1; i <= n; i++){while(stk.empty() == 0 && a[stk.top()] < a[i]){r[i] += w[stk.top()];stk.pop();}stk.push(i);}while(stk.empty() == 0){stk.pop();}for(int i = n; i >= 1; i--){while(stk.empty() == 0 && a[stk.top()] < a[i]){l[i] += w[stk.top()];stk.pop();}stk.push(i);}int maxi = -1e9;for(int i = 1; i <= n; i++){maxi = max(maxi, l[i] + r[i]);}cout << maxi;return 0;}```13 行未改动int main(){cin >> n;for(int i = 1; i <= n; i++){cin >> a[i] >> w[i];}for(int i = 1; i <= n; i++){while(stk.empty() == 0 && a[stk.top()] < a[i]){r[i] += w[stk.top()];stk.pop();}stk.push(i);}while(stk.empty() == 0){stk.pop();}for(int i = n; i >= 1; i--){while(stk.empty() == 0 && a[stk.top()] < a[i]){l[i] += w[stk.top()];stk.pop();}stk.push(i);}int maxi = -1e9;for(int i = 1; i <= n; i++){maxi = max(maxi, l[i] + r[i]);}cout << maxi;return 0;}```5 行未改动#include<bits/stdc++.h>#define int long longusing namespace std;int n;stack<int> stk;signed main(){cin >> n;int ans = 0;for(int i = 1; i <= n; i++){int x;cin >> x;while(stk.empty() == 0 && x >= stk.top()){stk.pop();}ans += stk.size();stk.push(x);}cout << ans;return 0;}```15 行未改动signed main(){cin >> n;for(int i = 1; i <= n; i++){cin >> a[i];pre[i] = pre[i - 1] + a[i];}for(int i = 1; i <= n; i++){while(stk.empty() == 0 && a[i] < a[stk.top()]){r[stk.top()] = i;stk.pop();}stk.push(i);}while(stk.empty() == 0){r[stk.top()] = n + 1;stk.pop();}for(int i = n; i >= 1; i--){while(stk.empty() == 0 && a[i] < a[stk.top()]){l[stk.top()] = i;stk.pop();}stk.push(i);}while(stk.empty() == 0){l[stk.top()] = 0;stk.pop();}int maxi = 0;for(int i = 1; i <= n; i++){int o = pre[r[i] - 1] - pre[l[i]];maxi = max(maxi, o * a[i]);}cout << maxi;return 0;}```19 行未改动20212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666713 行未改动818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271285 行未改动13413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616715 行未改动18318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823919 行未改动int main(){cin >> n;for(int i = 1; i <= n; i++){cin >> a[i] >> w[i];}for(int i = 1; i <= n; i++){while(stk.empty() == 0 && a[stk.top()] < a[i]){r[i] += w[stk.top()];stk.pop();}stk.push(i);}while(stk.empty() == 0){stk.pop();}for(int i = n; i >= 1; i--){while(stk.empty() == 0 && a[stk.top()] < a[i]){l[i] += w[stk.top()];stk.pop();}stk.push(i);}int maxi = -1e9;for(int i = 1; i <= n; i++){maxi = max(maxi, l[i] + r[i]);}cout << maxi;return 0;}```13 行未改动int main(){cin >> n;for(int i = 1; i <= n; i++){cin >> a[i] >> w[i];}for(int i = 1; i <= n; i++){while(stk.empty() == 0 && a[stk.top()] < a[i]){r[i] += w[stk.top()];stk.pop();}stk.push(i);}while(stk.empty() == 0){stk.pop();}for(int i = n; i >= 1; i--){while(stk.empty() == 0 && a[stk.top()] < a[i]){l[i] += w[stk.top()];stk.pop();}stk.push(i);}int maxi = -1e9;for(int i = 1; i <= n; i++){maxi = max(maxi, l[i] + r[i]);}cout << maxi;return 0;}```5 行未改动#include<bits/stdc++.h>#define int long longusing namespace std;int n;stack<int> stk;signed main(){cin >> n;int ans = 0;for(int i = 1; i <= n; i++){int x;cin >> x;while(stk.empty() == 0 && x >= stk.top()){stk.pop();}ans += stk.size();stk.push(x);}cout << ans;return 0;}```15 行未改动signed main(){cin >> n;for(int i = 1; i <= n; i++){cin >> a[i];pre[i] = pre[i - 1] + a[i];}for(int i = 1; i <= n; i++){while(stk.empty() == 0 && a[i] < a[stk.top()]){r[stk.top()] = i;stk.pop();}stk.push(i);}while(stk.empty() == 0){r[stk.top()] = n + 1;stk.pop();}for(int i = n; i >= 1; i--){while(stk.empty() == 0 && a[i] < a[stk.top()]){l[stk.top()] = i;stk.pop();}stk.push(i);}while(stk.empty() == 0){l[stk.top()] = 0;stk.pop();}int maxi = 0;for(int i = 1; i <= n; i++){int o = pre[r[i] - 1] - pre[l[i]];maxi = max(maxi, o * a[i]);}cout << maxi;return 0;}```