POJ2796 Feel Good 单调栈

9 篇文章 0 订阅
1 篇文章 0 订阅
Feel Good
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 15889 Accepted: 4394
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 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

Source




【题意】

qwq乍一看想在讲故事喔,模拟一下样例就知道,题目要求求一个区间,首先这个区间里的数都要是正数(当然),其次就是使这个区间的和乘这个区间的最小值的值最大,当然这个区间越长越好。

【题解】
说好的spj,可我感觉怎么不大对呢疑问...第一思路当然是枚举这个区间的最小值,然后往左往右扩展,直到找到比它小的值为止。
这样做显然有点暴力,那就可以想想单调栈啊~
对于每个数入栈,将栈顶所有比这个数大的数弹掉,并且更新入栈的数的左端点,出栈后栈顶的元素的右端点也是要更新的(每次)qwq。
最后再压入栈。
当然最后要把这个栈弹光a.并且更新栈顶的右端点w。
剩下只要枚举最小值用前缀和维护一下就可以啦~
咳咳问一下为什么把(sum*a[i]>ans)改成(sum*a[i]>=ans)就对了捏#


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
int n,top,ansl,ansr,a[100100],sta[100100];
long long s[100100],ans;
struct leo{
	int l,r;
}f[100100];
inline void push(int x){
	if(x==1){
		sta[++top]=x;
		return;
	}
	while(a[sta[top]]>=a[x] && top){
		f[x].l=f[sta[top]].l;
		f[sta[top-1]].r=f[sta[top]].r;
		top--;
	}
	f[sta[top]].r=f[x].r;
	sta[++top]=x;
}
inline void pop(){
	f[sta[top-1]].r=f[sta[top]].r;
	top--;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]),s[i]=s[i-1]+a[i],f[i].l=f[i].r=i;
	for(int i=1;i<=n;i++) push(i);
	while(top) pop();
	for(int i=1;i<=n;i++){
		long long sum=s[f[i].r]-s[f[i].l-1];
		if(sum*a[i]>=ans){ans=sum*a[i];ansl=f[i].l;ansr=f[i].r;}
	}
	printf("%lld\n%d %d\n",ans,ansl,ansr);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值