POJ 2796 Feel Good

传送门

Time Limit: 3000MS  Memory Limit: 65536K

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

Northeastern Europe 2005


Solution

单调栈


Implementation

#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."

时间: 2024-11-10 00:15:48

POJ 2796 Feel Good的相关文章

POJ 2796 Feel Good(单调栈)

题目地址:POJ 2796 单调栈的第一题就是这道..把我弄的晕头转向.现在终于明白了,对单调栈又加深了理解.原来单调栈不只是可以维护数. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #incl

Poj 2796 单调栈

关于单调栈的性质,和单调队列基本相同,只不过单调栈只使用数组的尾部, 类似于栈. Accepted Code: 1 /************************************************************************* 2 > File Name: 2796.cpp 3 > Author: Stomach_ache 4 > Mail: [email protected] 5 > Created Time: 2014年07月21日 星期一

Feel Good (poj 2796)

题目大意就是在给出的串中找出一段连续数字,使得 这一段的和 乘上 这一段最小的数 的结果最大. 可以用rmq做.每次区间找当中最小的数,算出值并记录位置.然后再递推它的左右区间. 不过- -,一开始用深搜递推RE了.栈空间不够了,然后慢慢优化,最后还是ac了. 貌似这一题是用单调栈做的,还可以用查并集做...我也是醉了 具体看代码吧 1 /************************************************************************* 2 > F

[poj 2796]单调栈

题目链接:http://poj.org/problem?id=2796 单调栈可以O(n)得到以每个位置为最小值,向左右最多扩展到哪里. #include<cstdio> #include<algorithm> #include<stack> using namespace std; const int maxn=100005; int a[maxn]; int l[maxn]; int r[maxn]; long long pre[maxn]; stack< p

51nod 1215 数组的宽度&amp;poj 2796 Feel Good(单调栈)

单调栈求每个数在哪些区间是最值的经典操作. 把数一个一个丢进单调栈,弹出的时候[st[top-1]+1,i-1]这段区间就是弹出的数为最值的区间. poj2796 弹出的时候更新答案即可 #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<algorithm> #include<queue> #include<cmath

POJ 2796 Feel Good(并查集)

题目链接:点击打开链接 思路: 该题转化一下, 就是枚举每一个数, 找到以这个数为最小值的最大区间(因为没有负数).  那么一个办法是预处理出每一个数左边第一个比他大的数的位置, 和右边第一个比他大的数的位置, 这个可以用构造单调栈的线性算法处理出来: 我们构造一个单调上升栈, 标记栈里每个元素在实际中的位置, 加入一个元素a[i]的时候, 如果栈顶元素大于他, 那么将栈顶元素出队列, i就是这个元素右边大于他的第一个元素.  直到栈顶元素<=a[i],这时,栈顶元素就是a[i]左边第一个>=

poj 2796 Feel Good (单调栈)

Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9778   Accepted: 2652 Case Time Limit: 1000MS   Special Judge Description Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated

poj 2796 Feel Good(单调栈)

Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11148   Accepted: 3059 Case Time Limit: 1000MS   Special Judge Description Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated

[ACM] POJ 2796 Feel Good (求序列中子序列的和与子序列中的最小数最大乘积)

Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9186   Accepted: 2509 Case Time Limit: 1000MS   Special Judge Description Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated