Feel Good

Posted -citywall123

tags:

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

Feel Good

Time Limit: 3000MS           Memory Limit: 65536KTotal Submissions: 11626           Accepted: 3212Case 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
题意:给出一个序列,要求的是一个区间,这个区间的最小值乘以这个区间数字的和 是最大值。求这个最大值与这个区间的位置。

 

单调栈

 

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<stack>
#define ll long long
using namespace std;
ll l[500005], r[500005], a[500005], sum[500005];
ll n,mx,l_pos,r_pos;
int main()

    freopen("joseph.in","r",stdin);
    freopen("feelgood.out","w",stdout);
    while (~scanf("%lld", &n))
    
        for (int i = 0; i < n; i++)//刚开始是从1开始输入的,然后前缀和也是从1开始处理的,一位可以简单一点,但是因为
                                  //栈是从0开始处理的,下面的a[p.top()]可能为a[0],导致一直TLE,找这个错误到自闭。。。
            scanf("%lld", &a[i]);
        
        sum[0] = a[0];
        for (int i = 1; i < n; i++)
        
            sum[i] = sum[i - 1] + a[i];
        
        stack<ll>p;
        for (int i = 0; i < n; i++)
        
            while (!p.empty() && a[p.top()] >= a[i])//按照大于等于a[i]的规则,从a[i]开始,向左边可以延伸最远的数的下标
                p.pop();
            if (p.empty())//说明a[i]左边的所有数都大于等于a[i]
                l[i] = 0;
            else//找到就记录从a[i]开始向左边寻找大于等于a[i]的最大边界下标
                l[i] = p.top() + 1;
            p.push(i);
        
        while (!p.empty())
            p.pop();
        for (int i = n - 1; i >= 0; i--)//往右边找第一个比a[i]小的数
        
            while (!p.empty() && a[p.top()] >= a[i])
                p.pop();
            if (p.empty())
                r[i] = n - 1;
            else
                r[i] = p.top() - 1;
            p.push(i);
        
        mx = 0;
        for (int i = 0; i < n; i++)
        
            if (a[i] >= 0)
            
                if(mx<a[i] * (sum[r[i]] - sum[l[i]] + a[l[i]]))
                
                    mx=a[i] * (sum[r[i]] - sum[l[i]] + a[l[i]]);
                    l_pos=l[i];
                    r_pos=r[i];
                
            
            
        

        printf("%lld\n", mx);
        printf("%lld %lld\n",l_pos+1,r_pos+1);
    
    return 0;

 

 

 

以上是关于Feel Good的主要内容,如果未能解决你的问题,请参考以下文章

Feel Good

Feel Good

POJ.2796feel good(单调栈)

Feel Good 单调递增栈+前缀和

POJ.2796feel good(单调栈)

POJ2796 Feel Good -- 单调队列