您的位置:首页 > 其它

poj-最多长方形数

2016-08-20 16:29 78 查看
题意:紧贴X轴有一些相互挨着的长方形,给定每个长方向的长和宽,求形成最大矩形是多少。

解法:堆栈,矩形依次入栈,保持高度递增。

#include<stdio.h>
#include<stdlib.h>
#include<stack>
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
int a;
int b;
} aa;
int main()
{
int t,n,m,k,tw,th,ll;
while(~scanf("%d",&t))
{
if(t==-1)
break;
int sum=0;
stack<node>ss;
ll=0;
while(t--)
{
scanf("%d%d",&aa.a,&aa.b);
if(aa.b>=ll)
ss.push(aa);
else
{
tw=0;
m=0;
while(!ss.empty()&&ss.top().b>>aa.b)
{
tw+=ss.top().a;
m=tw*ss.top().b;
if(m>sum)
sum=m;
ss.pop();
}
tw+=aa.a;
aa.a=tw;
ss.push(aa);
}
ll=aa.b;
}
tw=0;
m=0;
while(!ss.empty())
{
tw+=ss.top().a;
m=tw*ss.top().b;
if(m>sum)
sum=m;
ss.pop();
}
printf("%d\n",sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  堆栈