【POJ - 2796】【Feel Good 】

版权声明:本人原创,未经许可,不得转载 https://blog.csdn.net/qq_42505741/article/details/81952090

题目:

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

解题思路:就是一个人研究发现了一个规律,就是一个人一段时间的幸福值取决于他在一段时间内幸福值之和,并乘与区间内最小的幸福值,emmm,就酱,或许首想法就是暴力,然后光荣的T了,利用单调栈,来找到他左右第一个比他小的位置,这样就可以找出这个值能达到的最大面积。之后求和做乘积,然后循环取最大值,并且记录下标。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#define maxn 100100
using namespace std;
int num[maxn],loc1[maxn],loc2[maxn];
long long tt[maxn];
long long sum[maxn];
int n;
stack<int >q;
int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		
		for(int i=0;i<n;i++)
		{
			scanf("%d",&num[i]);
		}
		sum[0]=num[0];
		for(int i=1;i<n;i++)
		{
			sum[i]=sum[i-1]+num[i];
		}
		for(int i=0;i<n;i++)
		{
			while(q.size()&&num[q.top()]>=num[i])	q.pop();
			if(q.empty())
				loc1[i]=0;
			else
				loc1[i]=q.top()+1;
			q.push(i);
		}
		while(q.size()) q.pop();
		for(int i=n-1;i>=0;i--)
		{
			while(q.size()&&num[q.top()]>=num[i])	q.pop();
			if(q.empty())
				loc2[i]=n;
			else
				loc2[i]=q.top();
			q.push(i);
		}
//		for(int i=0;i<n;i++)
//			printf("%d ",loc1[i]);
//		for(int i=0;i<n;i++)
//			printf("%d ",loc2[i]);
//		printf("\n");
		for(int i=0;i<n;i++)
		{
			tt[i]=(sum[loc2[i]-1]-sum[loc1[i]-1])*num[i];
		}
		long long  manx=-1;
		int tab;
		for(int i=0;i<n;i++)
		{
			if(manx<tt[i])
			{
				manx=tt[i];
				tab=i;
			}	
		}
		cout<<manx<<endl;
		cout<<loc1[tab]+1<<" "<<loc2[tab]<<endl;
	}
	return 0;
}

ac代码:

猜你喜欢

转载自blog.csdn.net/qq_42505741/article/details/81952090