poj 2082——Terrible Sets

题意:求给出矩形中能找出最大矩形的面积

思路:给出n组wi,hi,每给出一组wi,hi相当于在沿x轴方向加上一个长wi,高hi的矩形。B集合中<x,y>即这些矩形中的点。T集合是横坐标从x0到x0+W,纵坐标从y0到yo+H中的点的集合,并且要求T集合中的点全部在B集合内。求满足条件的T集合中W*H最大的那一组。

代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

struct rec
{
    int w;
    int h;
};

rec s[50005];
int tot;

int main()
{
 //   freopen("data.txt","r",stdin);
    int n;
    while(scanf("%d",&n))
    {
        if(n==-1)break;
        tot=0;
        int ans=0;
        int last=0;
        for(int i=0;i<n;++i)
        {
            rec tmp;
            scanf("%d%d",&tmp.w,&tmp.h);
            if(tmp.h>=last)
            {
                s[tot++]=tmp;
                last=tmp.h;
                continue;
            }
            int width=0;
            while(tot>=0)
            {
                rec &top=s[tot-1];
       //         cout<<i<<' '<<tot<<' '<<top.h<<' '<<top.w<<endl;;
                if(top.h<=tmp.h||tot==0)
                {
                    s[tot].w=width+tmp.w;
                    s[tot].h=tmp.h;
   //                 cout<<"???"<<tot+1<<' '<<width<<' '<<tmp.h<<endl;
                    tot++;
                    break;
                }
                else{
                    width+=top.w;
                    ans=max(ans,width*top.h);
                    tot--;
                }
            }
            last=tmp.h;
        }
        int width=0;
  //      for(int i=0;i<tot;++i)cout<<s[i].w<<' '<<s[i].h<<endl;
        while(tot>0)
        {
      //      cout<<tot<<' '<<s[tot-1].w<<' '<<s[tot-1].h<<endl;
            width+=s[tot-1].w;
            ans=max(ans,width*s[tot-1].h);
            tot--;
        }
        printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值