BZOJ1657:[USACO2006MAR]Mooo

浅谈栈:https://www.cnblogs.com/AKMer/p/10278222.html

题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=1657

做两遍Look Up即可。

时间复杂度:\(O(n)\)

空间复杂度:\(O(n)\)

代码如下:

#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn=5e4+5;

int n,top,ans;
int h[maxn],v[maxn],sound[maxn],stk[maxn];

int read() {
	int x=0,f=1;char ch=getchar();
	for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
	for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
	return x*f;
}

int main() {
	n=read();
	for(int i=1;i<=n;i++)
		h[i]=read(),v[i]=read();
	for(int i=1;i<=n;i++) {
		while(top&&h[stk[top]]<=h[i])top--;
		sound[stk[top]]+=v[i],stk[++top]=i;
	}top=0;
	for(int i=n;i;i--) {
		while(top&&h[stk[top]]<=h[i])top--;
		sound[stk[top]]+=v[i],stk[++top]=i;
	}
	for(int i=1;i<=n;i++)
		ans=max(ans,sound[i]);
	printf("%d\n",ans);
	return 0;
}
posted @ 2019-01-16 20:20  AKMer  阅读(88)  评论(0编辑  收藏  举报