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

单调栈:保存一下;

#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=100001;
int s[maxn],a[maxn];
long long sum[maxn];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum[i]=sum[i-1]+a[i];
        }
        a[n+1]=-1;
        long long ans=-1,temp;
        int top=0,l,r;
        for(int i=1;i<=n+1;i++)
        {
            while(top!=0 && a[s[top]]>a[i])
            {
                temp=a[s[top]]*(sum[i-1]-sum[s[top-1]]);
                if(ans<temp)
                {
                    ans=temp;
                    l=s[top-1]+1;
                    r=i-1;
                }
                top--;
            }
            top++;
            s[top]=i;
        }
        printf("%lld\n%d %d\n",ans,l,r);
    }
    return 0;
}
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=100001;
struct S
{
    int num,id;
}s[maxn];
struct C
{
   int num,l,r;
}c[maxn];
int a[maxn];
long long sum[maxn];
int main()
{
    int n,top,i,j,l,r;
    long long ans=-1,temp;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            c[i].num=a[i];
            sum[i]=sum[i-1]+a[i];
        }
        s[0].num=-1;
        s[0].id=0;
        top=0;
        for(i=1;i<=n;i++)
        {
            for(j=top;j>=0;j--)
         {
            if(a[i]>s[j].num) break;
            else
            {
                c[s[j].id].l=i-1;
            }
         }
         s[j+1].num=a[i];
         s[j+1].id=i;
         top=j+1;
         c[i].r=s[top-1].id+1;
        }
        for(j=0;j<=top;j++)
            c[s[j].id].l=n;
        for(i=1;i<=n;i++)
        {
            temp=(sum[c[i].l]-sum[c[i].r-1])*c[i].num;
            if(ans<temp)
            {
                ans=temp;
                l=c[i].l;
                r=c[i].r;
            }
        }
       printf("%lld\n%d %d\n",ans,r,l);
    }
    return 0;
}
时间: 2024-10-13 06:41:01

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 3415:后缀数组+单调栈优化

题意很简单,求两个字符串长度大于等于K的子串个数 一开始还是只会暴力..发现n^2根本没法做...看了题解理解了半天才弄出来,太弱了... 思路:把两个字符串连接后做一次后缀数组,求出height 暴力的想法自然是枚举第一个子串的起始位置i和第二个子串的起始位置j,肯定会T的 看了题解才知道有单调栈这种有优化方法.. 将后缀分为A组(起始点为第一个字符串).B组 设符合要求的lcp长度为a,则其对答案的贡献为a-k+1(长度为k~a的都是符合要求的) 一开始这里我也是有疑问的,比如说k=1,aa

POJ 3415 后缀数组+单调栈

题目大意: 给定A,B两种字符串,问他们当中的长度大于k的公共子串的个数有多少个 这道题目本身理解不难,将两个字符串合并后求出它的后缀数组 然后利用后缀数组求解答案 这里一开始看题解说要用栈的思想,觉得很麻烦就不做了,后来在比赛中又遇到就后悔了,到今天看了很久才算看懂 首先建一个栈,从栈底到栈顶都保证是单调递增的 我们用一个tot记录当前栈中所有项和一个刚进入的子串匹配所能得到的总的子串的数目(当然前提是,当前进入的子串height值比栈顶还大,那么和栈中任意一个子串匹配都保持当前栈中记录的那时

[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

Poj 2796 单调栈

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

【POJ】2796:Feel Good【单调栈】

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

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(单调栈)

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

POJ 3415 Common Substrings(后缀数组+单调栈)

[题目链接] http://poj.org/problem?id=3415 [题目大意] 求出两个字符串长度大于k的公共子串的数目. [题解] 首先,很容易想到O(n2)的算法,将A串和B串加拼接符相连, 做一遍后缀数组,把分别属于A和B的所有后缀匹配,LCP-k+1就是对答案的贡献, 但是在这个基础上该如何优化呢. 我们可以发现按照sa的顺序下来,每个后缀和前面的串的LCP就是区间LCP的最小值, 那么我们维护一个单调栈,将所有单调递减的LCP值合并, 保存数量和长度,对每个属于B串的后缀更新