Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

ZOJ2642Feel Good

$
0
0
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;
}



作者:mowayao 发表于2013-12-3 0:16:16 原文链接
阅读:160 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>