POJ2796Feel Good[单调栈]

Posted Candy?

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2796Feel Good[单调栈]相关的知识,希望对你有一定的参考价值。

Feel Good
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 13376   Accepted: 3719
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 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

Source


题意:非负整数,求一个区间,区间和*区间最小值 最大

本题思想就是找以每个点为最小值的最长区间(也就是最大了)
找的方法就是单调栈
维护一个数值的递减栈,h数值,l是这个区间的和,b是开始位置(也就是向左延伸到的位置)
 
对于第i个数,先向左找,遇到更大的就用它更新答案并弹出它(因为它不可能再向右延伸了,最右到i-1)
然后插入这个数,并且插入的位置就是这个数最左的位置了
这个过程维护一个right,表示向右延伸的和
 
最后栈里的都是最右到n的
//
//  main.cpp
//  poj2796
//
//  Created by Candy on 10/5/16.
//  Copyright © 2016 Candy. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int N=1e5+5;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<0||c>9){if(c==-)f=-1;c=getchar();}
    while(c>=0&&c<=9){x=x*10+c-0;c=getchar();}
    return x;
}
int n;
ll a,ans=-1,ansb,anse;
struct data{
    ll h,l;
    int b;
}st[N];
int top=0;
int main(int argc, const char * argv[]) {
    n=read();
    for(int i=1;i<=n;i++){
        a=read();
        ll right=0;int b=i;
        while(top&&st[top].h>=a){
            ll tmp=(st[top].h*(st[top].l+right));
            right+=st[top].l;
            b=st[top].b;
            if(tmp>ans){
                ans=tmp;
                ansb=st[top].b;
                anse=i-1;//printf("t %lld %lld %lld\n",ans,ansb,anse);
            }
            top--;
        }
        top++;
        st[top].h=a;
        st[top].l=right+a;
        st[top].b=b;
    }
    ll right=0;
    while(top){
        ll tmp=(st[top].h*(st[top].l+right));
        right+=st[top].l;
        if(tmp>ans){
            ans=tmp;
            ansb=st[top].b;
            anse=n;
        }
        top--;
    }
    printf("%lld\n%lld %lld",ans,ansb,anse);
    return 0;
}

 

以上是关于POJ2796Feel Good[单调栈]的主要内容,如果未能解决你的问题,请参考以下文章

POJ.2796feel good(单调栈)

POJ2796Feel Good[单调栈]

POJ2796:Feel Good单调栈

poj2796 feel good 单调栈

poj2796 feel good 单调栈

POJ 2796 Feel Good(单调栈)