跳到正文

题解

B4273

hyk0YF0Gamer约 1 分钟
原文
文章信息与版本

文章信息

发布
2026年10月16日
收录
2026年10月19日
更新
2026年11月9日
篇幅
约 1 分钟
题目
B4273

版本

  1. v1当前

单调栈板子来了,做两遍单调栈,扫出每个点左边第一个比它小的数和右边第一个比它小的数,然后与该点高度相乘求max即可。

C++
#include<bits/stdc++.h>
using namespace std;
int n;
stack<int>s;
int a[1000005];
long long ans;
int l[1000005],r[1000005];
signed main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		while(!s.empty()&&a[s.top()]>a[i]){
			r[s.top()]=i-1;
			s.pop();
		}
		s.push(i);
	}
	while(!s.empty()){
		r[s.top()]=n;
		s.pop();
	}
	for(int i=n;i>=1;i--){
		while(!s.empty()&&a[s.top()]>a[i]){
			l[s.top()]=i+1;
			s.pop();
		}
		s.push(i);
	}
	while(!s.empty()){
		l[s.top()]=1;
		s.pop();
	}
	for(int i=1;i<=n;i++){
		ans=max(ans,1ll*a[i]*(r[i]-l[i]+1));
	}
	cout<<ans<<'\n';
	return 0;
}
原文