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 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<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<stack>
#define LL __int64
using namespace std;
struct node{
	LL num,pre,next,k;
};
LL a[100005],sum[100005];
int main()
{
	stack<node>Q;
	node tmp;
	int i,j,n;
	LL ans,sum1,x,y;
	while(scanf("%d",&n)!=EOF)
	{
		sum[0]=0;
		ans=sum1=-100;
		for(i=1;i<=n;i++)
		{
		scanf("%I64d",&a[i]);
		if(i==1)sum[i]=a[i];
		else sum[i]=sum[i-1]+a[i];
	}
	tmp.num=a[1];
	tmp.pre=1;
	tmp.next=1;
	tmp.k=1;
	Q.push(tmp);
	for(i=2;i<=n;i++)
	{
		node tmp1;
		tmp1.num=a[i];
		tmp1.pre=1;
		tmp1.next=1;
		tmp1.k=i;
		while(!Q.empty()&&tmp1.num<=Q.top().num)
		{
			tmp=Q.top();
			Q.pop();
			if(!Q.empty())
          Q.top().next+=tmp.next;
			tmp1.pre+=tmp.pre;
			ans=tmp.num*(sum[tmp.next+tmp.k-1]-sum[tmp.k-tmp.pre]);
			if(ans>=sum1){
				sum1=ans;
				x=tmp.k-tmp.pre+1;
				y=tmp.next+tmp.k-1;
			}
		}
		Q.push(tmp1);

	}
	while(!Q.empty()){
		tmp=Q.top();
		Q.pop();
		if(!Q.empty())
		Q.top().next+=tmp.next;
		ans=tmp.num*(sum[tmp.next+tmp.k-1]-sum[tmp.k-tmp.pre]);
		if(ans>=sum1){
			sum1=ans;
			x=tmp.k-tmp.pre+1;
			y=tmp.next+tmp.k-1;
		}
	}
	printf("%I64d\n%I64d %I64d\n",sum1,x,y);
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-22 01:58:29

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: 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 3415 Common Substrings(后缀数组+单调栈)

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