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.
A new idea Bill has recently developed assigns a non-negative integer value to each day of human life.
Bill calls this value the emotional value of the day. The greater the emotional value is, the better the daywas. Bill suggests that the value of some period of human life is proportional to the sum of the emotional values of the days in the given period, multiplied by the smallest emotional value of the day in it. This schema reflects that good on average period can be greatly spoiled by one very bad day.
Now Bill is planning to investigate his own life and find the period of his life that had the greatest value. Help him to do so.
A new idea Bill has recently developed assigns a non-negative integer value to each day of human life.
Bill calls this value the emotional value of the day. The greater the emotional value is, the better the daywas. Bill suggests that the value of some period of human life is proportional to the sum of the emotional values of the days in the given period, multiplied by the smallest emotional value of the day in it. This schema reflects that good on average period can be greatly spoiled by one very bad day.
Now Bill is planning to investigate his own life and find the period of his life that had the greatest value. Help him to do so.
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<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
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<sum1){
ans=sum1;
ansbegin=stack[top].begin;
ansend=stack[top].begin+stack[top].count-1;
}
top--;
}
top++;
stack[top].sum=a+tmp;
stack[top].minmum=a;
stack[top].count=count+1;
stack[top].begin=i-stack[top].count+1;
}
sum1=0;count=tmp=0;
while(top>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<sum1){
ans=sum1;
ansbegin=stack[top].begin;
ansend=stack[top].begin+stack[top].count-1;
}
top--;
}
printf("%lld
%lld %lld
",ans,ansbegin,ansend);
}
}
这题也可以用单调队列,其实单调队列更加好写,不容易写错,先从左到右建立一个严格单调递增的单调队列,计算出每一个数的左边界l[i],表示以这个数为最小值的区间,向左最多扩展的距离,同理可以得到r[i].
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 0x7fffffff
#define maxn 100050
ll a[maxn];
ll l[maxn],r[maxn];
ll sum[maxn];
ll q[1111111][2];
int main()
{
int n,m,i,j,T;
int front,rear;
while(scanf("%d",&n)!=EOF)
{
sum[0]=0;
for(i=1;i<=n;i++){
scanf("%lld",&a[i]);
sum[i]=sum[i-1]+a[i];
}
front=1;rear=0;
for(i=1;i<=n;i++){
while(front<=rear && q[rear][0]>=a[i]){
rear--;
}
if(front>rear)l[i]=1;
else{
l[i]=q[rear][1]+1;
}
rear++;
q[rear][0]=a[i];q[rear][1]=i;
}
front=1;rear=0;
for(i=n;i>=1;i--){
while(front<=rear && q[rear][0]>=a[i]){
rear--;
}
if(front>rear)r[i]=n;
else{
r[i]=q[rear][1]-1;
}
rear++;
q[rear][0]=a[i];q[rear][1]=i;
}
ll num=0;
ll ll,rr;
for(i=1;i<=n;i++){
if(a[i]*(sum[r[i] ]-sum[l[i]-1 ] )>=num){
ll=l[i];rr=r[i];
num=a[i]*(sum[r[i] ]-sum[l[i]-1 ] );
}
}
printf("%lld
",num);
printf("%lld %lld
",ll,rr);
}
return 0;
}