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

题意:找一个区间,使这个区间最小值乘上这个区间的和最大。

用单调栈维护递增,找出以每个$i$为最小值的最远的左右端点即可。

每次弹栈就更新被弹元素的右端点,入栈时更新入栈元素的左端点即可。

(为什么不把题说清楚有多组数据还有spj太垃圾了吧必须要区间最小!!!!)

(真的受不了了为什么poj那么卡aaaaa!!!浪费了我好多好多时间!!!!!!!!【粉转黑!!)

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;

LL a[100005], pre[100005];
int stk[100005], R[100005], L[100005], n;
int main() {
    while(~scanf("%d", &n)) {
        int top = 0;
        memset(pre, 0, sizeof(pre));
        memset(L, 0, sizeof(L));
        memset(R, 0, sizeof(R));
        for(int i = 1; i <= n; i ++) {
            scanf("%I64d", &a[i]);
            pre[i] = pre[i - 1] + a[i];
            while(a[i] < a[stk[top]] && top) {
                R[stk[top --]] = i - 1;
            }
            L[i] = stk[top] + 1;
            stk[++ top] = i;
        }
        while(top) {
            R[stk[top --]] = n;
        }
        LL ans = 0; int l, r;
        for(int i = 1; i <= n; i ++) {
            LL tmp = (pre[R[i]] - pre[L[i] - 1]) * a[i];
            if(tmp > ans) {
                ans = tmp;
                l = L[i], r = R[i];
            }
            if(tmp == ans) {
                if(R[i] - L[i] + 1 < r - l + 1) {
                    l = L[i], r = R[i];
                }
            }
        }
        printf("%I64d\n%d %d\n", ans, l, r);
    }

    return 0;
}

原文地址:https://www.cnblogs.com/wans-caesar-02111007/p/9818615.html

时间: 2024-10-01 05:01:45

【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日 星期一

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

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串的后缀更新