poj 2796 Feel Good(单调队列)

Feel Good
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 8598   Accepted: 2324
Case Time Limit: 1000MS   Special Judge

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.

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 10 6 - 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

Source

Northeastern Europe 2005

题意

给你n个数的数组arr[n]。要你找到一个L和R是的。(arr[L]+.....+arr[R])*arr[k]的值最大。arr[k]为L->R的数的最小值。

思路:

很好的单调队列题。只要理解单调队列(升序)的特性。

1,如果刚入队完第j个元素。单调队列里剩下的元素(下标i)一定是[i,j]的最小值。因为如果不是最小的话就会被后面入队的值覆盖。

2,在j入队的过程中如果停在k(原数组下标)的后面说明arr[k]比arr[j]小。说明。arr[j]是除了arr[k]是[k,j]间最小的。即[k+1,j]最小的。

3,在j入队的过程中如果队里的i(原数组下标)出队。说明arr[i]是[i,j-1]间最小的。因为一加入j它就被出队了。说明它已不是最小。

知道以上三点后就好办了。可以确定每个元素为区间的最小值的区间而区间和维护前缀和可以很快算出。

详细见代码:

#include<algorithm>
#include<iostream>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<queue>
#include<set>
#include<map>
//#pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
const double PI=acos(-1.0);
const int maxn=100010;
typedef __int64 ll;
int q[maxn];
int le[maxn],ri[maxn],em[maxn];
int head,tail;
ll sum[maxn];
int main()
{
    int n,i,ans;
    ll mv,tt;

    sum[0]=0;
    em[0]=-1;
    while(~scanf("%d",&n))
    {
        em[n+1]=-1;//为了清空队列
        for(i=1;i<=n;i++)
        {
            scanf("%d",&em[i]);
            sum[i]=sum[i-1]+em[i];
        }
        head=tail=0;
        q[tail++]=0;
        for(i=1;i<=n+1;i++)
        {
            while(head<tail&&em[i]<em[q[tail-1]])
            {
                ri[q[tail-1]]=i-1;//被出队右边界就可以确定
                tail--;
            }
            le[i]=q[tail-1]+1;//可以确定左边界
            q[tail++]=i;
        }
        mv=-1;
        for(i=1;i<=n;i++)
        {
            tt=(sum[ri[i]]-sum[le[i]-1])*em[i];
            if(tt>mv)
                mv=tt,ans=i;
        }
        printf("%I64d\n%d %d\n",mv,le[ans],ri[ans]);
    }
    return 0;
}


你可能感兴趣的:(c,算法,ACM)