您的位置:首页 > Web前端

POJ-2796-Feel Good-萌萌的单调栈

2015-08-26 09:12 316 查看
给一数组

设数组参考值为该区间和乘上区间内最小值

求数组内最大的参考值

用的也是单调栈的思想做的

这里区间和 先用前缀和预处理好了;

ps:看讨论区有人用什么rmq、dp+树状数组做的。待会也试试

<pre name="code" class="cpp">#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <set>
#include <vector>
using namespace std;
__int64 max (__int64 a,__int64 b)
{return a<b?b:a;}
struct node
{
	int w; 
	int s;
	int e;
};
node tm[100005];
__int64 top; 
__int64 anse,anss;
__int64 ans;
__int64 sum[100005]; 
 
int main()
{ 
	
	
	__int64 n,i;
	scanf("%I64d",&n);
	
	
	ans=top=0;
	node tt;
	for (i=1;i<=n;i++)
	{
		scanf("%d",&tt.w);
		sum[i]=sum[i-1]+tt.w; 
		tt.e=tt.s=i;
	 	while(top&& tm[top].w>=tt.w)
	{
		
		tm[top-1].e=tm[top].e;
		
		
		__int64 tol=(sum[tm[top].e]-sum[tm[top].s-1]) * tm[top].w;		//计算top处的区间参考值
		if (tol>=ans)
		{
			ans=tol;
			anss=tm[top].s;
			anse=tm[top].e;
		}
		tt.s=tm[top].s;
		top--;
	} 
	tm[++top]=tt;
	}
	
	while(top)
	{
		
		tm[top-1].e=tm[top].e; 
		__int64 tol=(sum[tm[top].e]-sum[tm[top].s-1]) *tm[top].w;
		if (tol>=ans)
		{
			ans=tol;
			anss=tm[top].s;
			anse=tm[top].e;
		}
		top--;
	}
	
	
	
	printf("%I64d\n%I64d %I64d\n",ans,anss,anse);
	
	
	return 0;
	
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: