标签:
传送门
Description
Input
Output
Sample Input
6 3 1 6 4 5 2
Sample Output
60 3 5
Source
#include <cstdio> #include <stack> #include <iostream> using namespace std; typedef long long LL; const int N(1e5+5); int h[N], L[N], R[N], st[N]; LL s[N]; int main(){ int n; scanf("%d", &n); for(int i=1; i<=n; i++) scanf("%d", h+i); for(int i=1; i<=n; i++) s[i]=s[i-1]+h[i]; // (L[i], R[i]] int t=-1; //top of stack for(int i=1; i<=n; i++){ for(; ~t && h[st[t]]>=h[i]; t--); L[i]=~t?st[t]:0; //error-prone st[++t]=i; } t=-1; for(int i=n; i; i--){ for(; ~t && h[st[t]]>=h[i]; t--); R[i]=~t?st[t]-1:n; st[++t]=i; } LL res=-1; //error-prone int l, r; for(int i=1; i<=n; i++){ LL t=h[i]*(s[R[i]]-s[L[i]]); if(t>res){ res=t; l=L[i]+1, r=R[i]; } } printf("%lld\n%d %d\n", res, l, r); return 0; }
代码里标error-prone的地方都是坑,请特别注意。
尤其 res应初始化成-1,因为"A new idea Bill has recently developed assigns a non-negative integer value to each day of human life."
标签:
原文地址:http://www.cnblogs.com/Patt/p/5068037.html