浅谈栈: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;
}

相关文章:

  • 2021-09-22
  • 2022-12-23
  • 2022-12-23
  • 2021-11-06
  • 2021-08-31
  • 2021-10-07
猜你喜欢
  • 2022-01-08
  • 2022-12-23
  • 2021-10-02
  • 2022-02-22
  • 2021-10-23
  • 2022-12-23
  • 2021-07-17
相关资源
相似解决方案