Description
Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated to studying how good or bad days influent people's memories about some period of life.Input
The first line of the input contains n - the number of days of Bill's life he is planning to investigate(1 <= n <= 100 000). The rest of the file contains n integer numbers a1, a2, ... an ranging from 0 to 106 - the emotional values of the days. Numbers are separated by spaces and/or line breaks.Output
Print the greatest value of some period of Bill's life in the first line. And on the second line print two numbers l and r such that the period from l-th to r-th day of Bill's life(inclusive) has the greatest possible value. If there are multiple periods with the greatest possible value,then print any one of them.Sample Input
6 3 1 6 4 5 2
Sample Output
60
3 5
這題是用單調棧,題意是給你一段區間,求出(在這段區間之類的最小值*這段區間所有元素之和)的最大值。可以維護一個單調遞增隊列,記錄棧內的sum(區間所有元素的和),minmum(區間最小值),begin(區間的最前面一個數的位置),count(區間的長度)。每讀入一個數,把棧內所有大於等於它的數都刪掉,這裡刪掉的時候注意要把刪掉的數所對應的區間的長度以及和傳到棧內下面的一個數所對應的區間,因為這個數要比下面的數大,所以對下面那個數來說,刪掉的數所對應的區間也可以包含進來。當所有的數都循環完後,再從棧頂開始向下刪除數,同時把這個數的信息傳到下一個數,可以發現兩個操作在這裡是一樣的。
#include#include #include #include #include using namespace std; #define maxn 100060 #define ll long long struct node{ ll sum,minmum,begin,count; }stack[maxn]; int main() { //int n,m,i,j,top; int n,m,i,j; ll ans,sum1,tmp,count,a,ansbegin,ansend,top; while(scanf(%d,&n)!=EOF) { ansbegin=ansend=1; top=0;ans=0; for(i=1;i<=n;i++){ scanf(%lld,&a); sum1=0;count=tmp=0; while(top>0 && stack[top].minmum>=a){ stack[top].count+=count; count=stack[top].count; stack[top].sum+=tmp; tmp=stack[top].sum; sum1=stack[top].sum*stack[top].minmum; if(ans 0){ stack[top].count+=count; count=stack[top].count; stack[top].sum+=tmp; tmp=stack[top].sum; sum1=stack[top].sum*stack[top].minmum; if(ans