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

本文介绍了一种利用单调栈思想解决最大参考值问题的方法。通过预处理区间和使用结构体记录栈中元素的权值、起始及结束位置,实现了高效地找到数组中最大的参考值及其对应的区间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给一数组
设数组参考值为  该区间和乘上区间内最小值
求数组内  最大的参考值 
用的也是单调栈的思想做的
这里区间和 先用前缀和预处理好了;


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;
	
} 




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值