HDU 1506 单调队列

#include<iostream>
#include<stack>
#include<algorithm>
using namespace std;

typedef long long LL;
const int N = 100000 + 50;
stack<int> st;
LL h[N];
int r[N],l[N];
int n;

int main()
{
	while(cin>>n && n){
		for(int i = 0;i < n;i ++) cin>>h[i];
		while(st.size()) st.pop();
		for(int i = 0;i < n;i ++){
			while(st.size() && h[st.top()] >= h[i]) st.pop();
			if(st.empty()) l[i] = 0;
			else           l[i] = st.top()+1;
			st.push(i);
		}
		while(st.size()) st.pop();
		for(int i = n-1;i >= 0;i --){
			while(st.size() && h[st.top()] >= h[i]) st.pop();
			if(st.empty()) r[i] = n;
			else           r[i] = st.top();
			st.push(i);
		}
		LL ans = 0;
		for(int i = 0;i < n;i ++){
			ans = max(ans,h[i]*(r[i]-l[i]));
		}
		cout<<ans<<endl;
	}	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41988889/article/details/89047598