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

一开始做时百思不得其解,于是打了个暴力居然有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;
}

猜你喜欢

转载自blog.csdn.net/Rem_Inory/article/details/81808752