ZOJ2642Feel Good

原创 2013年12月03日 00:16:16
Feel Good

Time Limit: 10 Seconds      Memory Limit: 32768 KB      Special Judge

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

There are several test cases in the input file. The first line of each case 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. Proceed to the end of file.

Output

For each test case, 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


因为ai 是大于等于0的,可以枚举每个点作为最小值时的最长区间,用单调栈(单调队列)来维护:

如样例: 3 1 6 4 5 2

以左边为例:

一开始栈里放的是 0 (a0 = -1)(数组下标)

a1 = 3 > -1  1入栈  a1的最长区间左边就取1 

a2 = 1 < 3  1出栈 a2 > -1 , 取1(可以理解为空栈) 2入栈

a3 = 6 > 1   取3  3入栈

a4 = 4 < 6  3出栈 a4 > 1   取3(栈顶+1)4入栈

a5 = 5 > 4  5入栈  取 5

a6 = 2 < 5  5出栈  a6 < 4 4出栈  a6 > 1 6入栈  取3  6入栈


弹了好几次才AC的 ,然后效率刷进第三咯,LOL。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
const int maxn = 100000+10;
long long num[maxn];
long long sum[maxn],ans;
int n;
int next[maxn],last[maxn];
stack<long long> stk,sk;
void init(){
    ans = -1;
    sum[0] = 0;
    while(!stk.empty()) stk.pop();
    while(!sk.empty()) sk.pop();
}
int main(){
	while(~scanf("%d",&n)){
        init();
        for(int i = 1; i <= n; i++){
            scanf("%lld",&num[i]);
            sum[i] = sum[i-1] + num[i];
        }
        stk.push(0);
        num[0] = -100;
        for(int i = 1; i <= n; i++){
          	if(num[i]>num[stk.top()]){
                stk.push(i);
                last[i] = i;
            }else{
                while(num[stk.top()]>=num[i])  stk.pop();
                if(stk.top()==0)
                    last[i] = 1;
                else
                    last[i] = stk.top()+1;
                stk.push(i);
            }
        }
        sk.push(0);
        for(int i = n; i >= 1; i--){
          	if(num[i]>num[sk.top()]){
                sk.push(i);
                next[i] = i;
            }else{
                while(num[sk.top()]>=num[i]) sk.pop();
                if(sk.top()== 0)
                    next[i] = n;
                else
                    next[i] = sk.top()-1;
                sk.push(i);
            }
        }
        int sta = 1,end = 1;
        for(int i = 1; i <= n; i++){
            long long tmp = (sum[next[i]]-sum[last[i]-1])*num[i];
          	if(ans < tmp){
                ans = tmp;
                end = next[i];
                sta = last[i];
            }
        }
        printf("%lld\n%d %d\n",ans,sta,end);
		}

    return 0;
}



Feel Good(单调栈(比之前更加完善的模板))

Link:http://poj.org/problem?id=2796 Feel Good Time Limit: 3000MS   Memor...
  • Enjoying_Science
  • Enjoying_Science
  • 2015年08月23日 20:51
  • 641

ZOJ题目分类

ZOJ题目分类 初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 13...
  • hitwengqi
  • hitwengqi
  • 2012年11月13日 21:18
  • 6284

ZOJ,PKU--训练题分类

ZOJ题目分类  初学者题:  1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 ...
  • shao1996
  • shao1996
  • 2016年05月27日 21:43
  • 711

POJ 2796:Feel Good(单调栈)

Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12926   Accepted: 3617 ...
  • Jaihk662
  • Jaihk662
  • 2016年08月09日 00:25
  • 400

UVA - 1619 Feel Good(高效算法:滑动窗口)

题意大致是从一个数列里找一个最短的子序列,使得这个子序列的最小值乘这个子序列的和最大。 这道题最关键的地方是找到数列中的某个数在哪个连续序列里是最小的,即这个数的统治区域,一开始我想到的就是输入完之后...
  • qazwer1098
  • qazwer1098
  • 2015年05月03日 17:40
  • 3481

N元语法模型的数据稀疏问题解决方法之一:Good-Turing平滑

在统计语言模型章节中,我们谈到了N元语法模型不可避免的一个问题,就是数据稀疏,其原因是大规模语料统计与有限语料的矛盾。根据齐普夫(Zipf)法则,我们能够推测知零概率问题不可避免。数据稀疏问题的解决办...
  • u010189459
  • u010189459
  • 2014年07月28日 20:57
  • 2017

POJ - 1017 (贪心)

POJ - 1017 (贪心) A factory produces products packed in square packets of the same height h and of th...
  • zhaiqiming2010
  • zhaiqiming2010
  • 2017年03月16日 00:21
  • 211

C语言算法基础

C语言算法基础            算法(Algorithm):计算机解题的基本思想方法和步骤算法的描述:是对要解决一个问题或要完成一项任务所采取的方法和步骤的描述...
  • zhang63020936
  • zhang63020936
  • 2014年12月19日 09:03
  • 836

1619 - Feel Good 【滑动窗口】

题目大意 样例 input output 解释 思路 代码 Hit题目大意传送门一组样例中有n(n...
  • u010468553
  • u010468553
  • 2017年05月16日 17:56
  • 85

POJ Feel Good (单调栈)

Bill is developing a new mathematical theory for human emotions. His recent investigations are dedic...
  • qq_39091609
  • qq_39091609
  • 2017年07月17日 23:19
  • 34
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ZOJ2642Feel Good
举报原因:
原因补充:

(最多只允许输入30个字)