Gym - 101334F 单调栈
生活随笔
收集整理的這篇文章主要介紹了
Gym - 101334F 单调栈
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
當時我的第一想法也是用單調棧,但是被我寫炸了;我也不知道錯在哪里;
看了大神的寫法,用數組模擬的;
記錄下單調遞增棧的下標,以及每個數字作為最小值的最左邊的位置。
當有數據要出棧的時候,說明棧里的數據已經不是最小了,右端點就是當前位置-1,那么就可以計算棧頂的元素所作的貢獻;出棧完后,當前這個數字,他的最左邊就是棧頂所能到達的位置;入棧;
#include <bits/stdc++.h>using namespace std;const int maxn = 100000 + 5; int a[maxn]; int stacks[maxn]; long long sum[maxn]; int lef[maxn];int main() {freopen("feelgood.in","r",stdin);freopen("feelgood.out","w",stdout);int n;scanf("%d",&n);memset(sum,0,sizeof(sum));memset(lef,0,sizeof(lef));for(int i=1;i<=n;i++) {scanf("%d",&a[i]);sum[i] = sum[i-1] + a[i];}a[++n] = -1;int top = 0;long long ans = -1;int ansl = 0,ansr = 0;for(int i=1;i<=n;i++) {if(top==0||a[i]>a[stacks[top-1]]) {stacks[top++] = i;lef[i] = i;continue;}if(a[i]==a[stacks[top-1]])continue;while(top>=1&&a[i]<a[stacks[top-1]]) {top --;long long tmp = (long long)a[stacks[top]]*(sum[i-1]-sum[lef[stacks[top]]-1]);if(tmp>ans) {ansr = i-1;ansl = lef[stacks[top]];ans = tmp;}}lef[i] = lef[stacks[top]];stacks[top++] = i;}printf("%lld\n%d %d\n",ans,ansl,ansr);return 0; } View Code?
(之前的錯誤找到了ans=-1,可以都為0)
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 int n; 6 const int maxn = 100000+5; 7 struct num { 8 long long value; 9 int maxleft,maxright; 10 int minleft,minright; 11 num():maxleft(1),maxright(1),minleft(1),minright(1){} 12 }a[maxn]; 13 14 stack<pair<int,int> > S; 15 16 long long sum[maxn]; 17 18 void getMax() 19 { 20 while(!S.empty()) 21 S.pop(); 22 S.push(make_pair(a[0].value,0)); 23 for(int i=1;i<n;i++) { 24 while(!S.empty()&&S.top().first<=a[i].value) { 25 //int value = S.top().first; 26 int key = S.top().second; 27 S.pop(); 28 29 a[i].maxleft +=a[key].maxleft; 30 if(!S.empty()) { 31 a[S.top().second].maxright +=a[key].maxright; 32 } 33 } 34 S.push(make_pair(a[i].value,i)); 35 } 36 while(!S.empty()) { 37 int key = S.top().second; 38 S.pop(); 39 if(!S.empty()) { 40 a[S.top().second].maxright +=a[key].maxright; 41 } 42 } 43 } 44 45 void getMin() 46 { 47 while(!S.empty()) 48 S.pop(); 49 S.push(make_pair(a[0].value,0)); 50 for(int i=1;i<n;i++) { 51 while(!S.empty()&&S.top().first>=a[i].value) { 52 //int value = S.top().first; 53 int key = S.top().second; 54 S.pop(); 55 56 a[i].minleft +=a[key].minleft; 57 if(!S.empty()) { 58 a[S.top().second].minright +=a[key].minright; 59 } 60 } 61 S.push(make_pair(a[i].value,i)); 62 } 63 while(!S.empty()) { 64 int key = S.top().second; 65 S.pop(); 66 if(!S.empty()) { 67 a[S.top().second].minright +=a[key].minright; 68 } 69 } 70 } 71 72 int main() 73 { 74 freopen("feelgood.in","r",stdin); 75 freopen("feelgood.out","w",stdout); 76 scanf("%d",&n); 77 for(int i=0;i<n;i++) { 78 scanf("%lld",&a[i].value); 79 sum[i+1] = sum[i] + a[i].value; 80 } 81 // getMax(); 82 getMin(); 83 84 int l = 0; 85 int r = 0; 86 long long ans = -1; 87 for(int i=0;i<n;i++) { 88 long long tmp = a[i].value*(sum[i+a[i].minright]-sum[i-a[i].minleft+1]); 89 if(ans<tmp) { 90 ans = tmp; 91 l = i - a[i].minleft + 2; 92 r = i + a[i].minright; 93 } 94 } 95 96 printf("%lld\n%d %d\n",ans,l,r); 97 98 return 0; 99 } View Code?
轉載于:https://www.cnblogs.com/TreeDream/p/6937770.html
總結
以上是生活随笔為你收集整理的Gym - 101334F 单调栈的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 导出csv
- 下一篇: php date strtotime的用