【POJ】2796 Feel Good

【POJ】2796 Feel Good

Time Limit: 3000MS
Memory Limit: 65536K
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

翻译

感觉不错

时限: 3000MS
内存限制: 65536K
案例时间限制:1000MS
特别评测

描述

比尔正在为人类的情感发展一种新的数学理论。他最近的调查致力于研究好或坏的日子如何影响人们对某个时期的记忆。

比尔最近提出了一个新想法,即为人类的每一天分配一个非负整数值。

比尔称此价值为当日的情感价值。情感价值越大,日子越好。比尔建议,生命中某个时期的价值与给定时期中某天的情感价值之和成正比,再乘以该天中某天的最小情感价值。这种模式反映出,在一个非常糟糕的日子里,平均期间的良好状况可能会大大受损。

现在,比尔正计划研究自己的生活,并找到人生中最有价值的时期。帮助他这样做。

输入

输入的第一行包含n-他计划调查的Bill的寿命天数(1 <= n <= 100 000)。文件的其余部分包含n个整数a1,a2,…,范围从0到10 6-天的情感值。数字由空格和/或换行符分隔。

输出

在第一行中打印比尔一生中某个时期的最大值。在第二行上打印两个数字l和r,以使Bill生命中第l天到第r天(含)的时间段具有最大可能的值。如果存在多个具有最大可能值的期间,则打印其中任何一个。

样本输入

6
3 1 6 4 5 2

样本输出

60
3 5

思路

CodeForce817DImbalanced Array类似。
先求前缀和,
往左右找最小到的位置。
找最大值(当前数*前缀和的差)

代码

#include<iostream>
#include<cstdio>
using namespace std;
long long sum[1000010],a[1000010],pos[1000010],l[1000010],r[1000010];
int main()
{
	long long n,i,len,ans,ansl,ansr;
	scanf("%lld",&n);
	for(sum[0]=0,i=1;i<=n;scanf("%lld",&a[i]),sum[i]=sum[i-1]+a[i],i++);
	a[0]=a[n+1]=-100000000000;
	for(len=0,pos[0]=0,i=1;i<=n;i++)//往左找最小到的位置. 
	{
		for(;a[i]<=a[pos[len]];len--);
		if(len==0) l[i]=1;
		else l[i]=pos[len]+1;
		pos[++len]=i;
	}
	for(len=0,pos[0]=n+1,i=n;i>0;i--)//往右找最小到的位置. 
	{
		for(;a[i]<=a[pos[len]];len--);
		if(len==0) r[i]=n;
		else r[i]=pos[len]-1;
		pos[++len]=i;
	}
	for(ans=-1,i=1;i<=n;i++)//找最大值 
		if(ans<a[i]*(sum[r[i]]-sum[l[i]-1]))
		{
			ans=a[i]*(sum[r[i]]-sum[l[i]-1]);
			ansl=l[i];
			ansr=r[i];
		}
	printf("%lld\n%lld %lld\n",ans,ansl,ansr);
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值