分析:这个题的关键是要找到,当某个值是最小值时它最大的影响区间时什么。可以通过单调队列(单调栈)在nlogn的时间内实现
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <queue> 6 using namespace std; 7 const int maxn=100000+100; 8 int n; 9 int a[maxn],L[maxn],R[maxn]; 10 long long sum[maxn]; 11 int main(){ 12 int t=0; 13 //freopen("out.txt","w",stdout); 14 while(scanf("%d",&n)!=EOF){ 15 if(t)printf(" "); 16 t++; 17 sum[0]=0; 18 for(int i=1;i<=n;i++){ 19 scanf("%d",&a[i]); 20 sum[i]=sum[i-1]+a[i]; 21 } 22 deque<int>q; 23 for(int i=1;i<=n;i++){ 24 while(!q.empty()&&a[i]<=a[q.back()]){ 25 R[q.back()]=i-1; 26 q.pop_back(); 27 } 28 if(q.empty())L[i]=1; 29 else L[i]=q.back()+1; 30 q.push_back(i); 31 } 32 while(!q.empty()){ 33 R[q.back()]=n; 34 q.pop_back(); 35 } 36 long long ans=0; 37 int ansL=1,ansR=1; 38 for(int i=1;i<=n;i++){ 39 if(ans<(long long)(sum[R[i]]-sum[L[i]-1])*a[i]){ 40 ans=(long long)(sum[R[i]]-sum[L[i]-1])*a[i]; 41 ansL=L[i],ansR=R[i]; 42 } 43 } 44 printf("%lld ",ans); 45 printf("%d %d ",ansL,ansR); 46 } 47 return 0; 48 }