https://cn.vjudge.net/problem/POJ-2559
#include <iostream>
#include <algorithm>
#include<stack>
using namespace std;
#define ll long long
typedef pair<ll,ll> pll;
stack<pll> s;
int main()
{
ll n,m,i,j,ans,x;
while(scanf("%lld",&n) != EOF&&n){
ll MAX = 0;
for(i=1;i<=n;i++)
{
scanf("%lld",&x);
ll w = 0;
while(!s.empty()&&s.top().first >= x)
{
ll tmph = s.top().first;
ll tmpw = s.top().second;
s.pop();
w += tmpw;
MAX = max(MAX,tmph*w);
}
s.push(make_pair(x,w+1));
}
int tmp = 0;
while(!s.empty())
{
MAX = max(MAX,s.top().first*(tmp+s.top().second));
tmp += s.top().second;
s.pop();
}
printf("%lld\n",MAX);
}
return 0;
}
本文提供了一种解决 POJ-2559 编程题目的算法实现,通过使用堆栈和对输入数据进行处理,有效地求解了最大矩形面积问题。代码采用 C++ 实现,利用 pair 和 stack 类型进行数据存储。
378

被折叠的 条评论
为什么被折叠?



