poj 2082——Terrible Sets

来源:互联网 发布:mysql update join on 编辑:程序博客网 时间:2024/04/27 20:18

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

思路:给出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;}


0 0
原创粉丝点击