【题解】洛谷P1901 发射站(栈 单调队列)

3 篇文章 0 订阅

一开始做时百思不得其解,于是打了个暴力居然有60分。。

正解其实挺好理解的,不过我想不到、。。

附链接

https://www.luogu.org/blog/user41569/solution-p1901

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#define ll long long
using namespace std;
const int maxn=1000010;
int n;
struct tower
{
	ll h;
	int w; //energy
	int num; //id
	int same; //same height
}a[maxn];
ll ans[maxn];
ll h; //height
int k; //energy 
int top; //top element of this stack
int main()
{
	scanf("%d",&n);
	scanf("%lld%d",&a[++top].h,&a[1].w);
	a[1].num=1;
	a[1].same=1;
	for(int i=2;i<=n;i++)
	{
		scanf("%lld%d",&h,&k);
		while(top!=0&&a[top].h<h)
		{
			ans[i]+=a[top].w;
			top--;
		}
		int j=top;
		if(a[j].h==h)
		{
			j-=a[j].same;
			a[top+1].same=a[top].same+1;
			a[top+1].h=h;
			a[top+1].w=k;
			a[++top].num=i;
		}
		else
		{
			a[top+1].h=h;
			a[top+1].w=k;
			a[top+1].num=i;
			a[++top].same=1;
		}
		ans[a[j].num]+=k;
	}
	ll maxx=-1e9;
	for(int i=1;i<=n;i++)
	{
		maxx=max(maxx,ans[i]);
	}
	printf("%lld",maxx);
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值