强行修炼单调栈系列
呵呵哒 真的是强行修炼受了内伤 我需要冷静 代码如下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<map> 3 //#include<bits/stdc++.h> 4 #include<vector> 5 #include<stack> 6 #include<iostream> 7 #include<algorithm> 8 #include<cstring> 9 #include<cmath> 10 #include<queue> 11 #include<cstdlib> 12 #include<climits> 13 #define PI acos(-1.0) 14 #define INF 0x3f3f3f3f 15 using namespace std; 16 typedef long long ll; 17 typedef __int64 int64; 18 const ll mood=1e9+7; 19 const int64 Mod=998244353; 20 const double eps=1e-9; 21 const int N=2e7+10; 22 const int MAXN=1e5+5; 23 inline void rl(ll&num){ 24 num=0;ll f=1;char ch=getchar(); 25 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 26 while(ch>='0'&&ch<='9')num=num*10+ch-'0',ch=getchar(); 27 num*=f; 28 } 29 inline void ri(int &num){ 30 num=0;int f=1;char ch=getchar(); 31 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 32 while(ch>='0'&&ch<='9')num=num*10+ch-'0',ch=getchar(); 33 num*=f; 34 } 35 int getnum()//相邻的个位整数输入 如想分别保存1234 输入连续的1234 a[i]=getnum();就可以实现 36 { 37 char ch=getchar(); 38 while((ch<'0' || ch>'9') && ch!='-') 39 ch=getchar(); 40 return (ch-'0'); 41 } 42 inline void out(int x){ if(x<0) {putchar('-'); x*=-1;}if(x>9) out(x/10); putchar(x%10+'0'); } 43 struct node{ 44 ll num,p,n; 45 ll now; 46 }; 47 ll st[MAXN],t[MAXN]; 48 int main() 49 { 50 51 int n; 52 while(scanf("%d",&n)!=EOF) 53 { 54 stack<node>s; 55 node tem; 56 ll ans=-100,sum=-100; 57 st[0]=0; 58 for(int i=1;i<=n;i++) 59 { 60 scanf("%lld",t+i); 61 st[i]=st[i-1]+t[i]; 62 } 63 tem.num=t[1];tem.p=1;tem.n=tem.now=1; 64 s.push(tem); 65 ll x=0,y=0; 66 for(int i=2;i<=n;i++) 67 { 68 node temp; 69 temp=(node){t[i],1,1,i}; 70 while(!s.empty()&&temp.num<=s.top().num) 71 { 72 tem=s.top(); 73 s.pop(); 74 if(!s.empty()) s.top().n+=tem.n; 75 temp.p+=tem.p; 76 ans=tem.num*(st[tem.now+tem.n-1]-st[tem.now-tem.p]); 77 if(ans>sum) 78 { 79 sum=ans; 80 x=tem.now-tem.p+1; 81 y=tem.now+tem.n-1; 82 } 83 } 84 s.push(temp); 85 } 86 while(!s.empty()) 87 { 88 tem=s.top(); 89 s.pop(); 90 if(!s.empty()) s.top().n+=tem.n; 91 ans=tem.num*(st[tem.now+tem.n-1]-st[tem.now-tem.p]); 92 if(ans>sum) 93 { 94 sum=ans; 95 x=tem.now-tem.p+1; 96 y=tem.now+tem.n-1; 97 } 98 } 99 cout<<sum<<endl<<x<<' '<<y<<endl; 100 } 101 return 0; 102 }