usaco1.4.1(packrec)

题目:

Packing Rectangles
IOI 95
 
The six basic layouts of four rectangles

Four rectangles are given. Find the smallest enclosing (new) rectangle into which these four may be fitted without overlapping. By smallest rectangle, we mean the one with the smallest area.

All four rectangles should have their sides parallel to the corresponding sides of the enclosing rectangle. Figure 1 shows six ways to fit four rectangles together. These six are the only possible basic layouts, since any other layout can be obtained from a basic layout by rotation or reflection. Rectangles may be rotated 90 degrees during packing.

There may exist several different enclosing rectangles fulfilling the requirements, all with the same area. You must produce all such enclosing rectangles.

PROGRAM NAME: packrec

INPUT FORMAT

Four lines, each containing two positive space-separated integers that represent the lengths of a rectangle's two sides. Each side of a rectangle is at least 1 and at most 50.

SAMPLE INPUT (file packrec.in)

1 2
2 3
3 4
4 5

OUTPUT FORMAT

The output file contains one line more than the number of solutions. The first line contains a single integer: the minimum area of the enclosing rectangles. Each of the following lines contains one solution described by two numbers p and q with p<=q. These lines must be sorted in ascending order of p, and must all be different.

SAMPLE OUTPUT (file packrec.out)

40
4 10
5 8


这个题目我没什么说的,卡了3天,看完解题报告后又卡了2天。

解题报告说4个方片只有图上的六种摆法(其他的都可以通过这六种转化),直接枚举这六种(其实是五种)。

看完解题报告还Wa,原因是虽然只有5中摆法(也就是位置关系),但每一个方块交换长宽又有两种状态,这点没考虑到。在中午想到这点后,下午写了出来就过了。

这种题目真吐血,没什么好说的了。想爆粗口了,TMD。

代码:

/*
ID:614433244
PROG: packrec
LANG: C++
*/

#include"iostream"
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;

struct M
{
    int v,a,b;
}ans[240*16];
M t;
int r;
void push( M t )
{
    while( t.v<ans[r].v && r>-1 )
        r--;
    if( r==-1 )
    {
        r++;
        ans[r]=t;
    }
    else
    {
        if( ans[r].v==t.v )
        {
            r++;
            ans[r]=t;
        }
    }
//cout<<ans[r].v<<" "<<ans[r].a<<" "<<ans[r].b<<endl;
}

bool cmp( M a,M b )
{
    return a.a<b.a;
}
struct N
{
    int a,b;
}jx[6];
int max( int a,int b,int c,int d )
{
    a=a>b?a:b;
    c=c>d?c:d;
    return a>c?a:c;
}
int main()
{
    freopen("packrec.in","r",stdin);
    freopen("packrec.out","w",stdout);
    r=-1;
    int i,j,k;
    for( i=0;i<4;i++ )
        scanf("%d%d",&jx[i].a,&jx[i].b);
    int l[4],w[4];//分别表示四个矩形的横向和纵向长度
    int ll,ww;
//l表示横向长度,w表示纵向长度,题目图中给的只是位置关系,还得按长短分
    int p;
    for( p=0;p<16;p++ )
    {
        if( p==0 )
        {
            l[0]=jx[0].a;w[0]=jx[0].b;
            l[1]=jx[1].a;w[1]=jx[1].b;
            l[2]=jx[2].a;w[2]=jx[2].b;
            l[3]=jx[3].a;w[3]=jx[3].b;
        }
        if( p==1 )
        {
            l[0]=jx[0].b;w[0]=jx[0].a;
            l[1]=jx[1].a;w[1]=jx[1].b;
            l[2]=jx[2].a;w[2]=jx[2].b;
            l[3]=jx[3].a;w[3]=jx[3].b;
        }
        if( p==2 )
        {
            l[0]=jx[0].a;w[0]=jx[0].b;
            l[1]=jx[1].b;w[1]=jx[1].a;
            l[2]=jx[2].a;w[2]=jx[2].b;
            l[3]=jx[3].a;w[3]=jx[3].b;
        }
        if( p==3 )
        {
            l[0]=jx[0].b;w[0]=jx[0].a;
            l[1]=jx[1].b;w[1]=jx[1].a;
            l[2]=jx[2].a;w[2]=jx[2].b;
            l[3]=jx[3].a;w[3]=jx[3].b;
        }
        if( p==4 )
        {
            l[0]=jx[0].a;w[0]=jx[0].b;
            l[1]=jx[1].a;w[1]=jx[1].b;
            l[2]=jx[2].b;w[2]=jx[2].a;
            l[3]=jx[3].a;w[3]=jx[3].b;
        }
        if( p==5 )
        {
            l[0]=jx[0].b;w[0]=jx[0].a;
            l[1]=jx[1].a;w[1]=jx[1].b;
            l[2]=jx[2].b;w[2]=jx[2].a;
            l[3]=jx[3].a;w[3]=jx[3].b;
        }
        if( p==6 )
        {
            l[0]=jx[0].a;w[0]=jx[0].b;
            l[1]=jx[1].b;w[1]=jx[1].a;
            l[2]=jx[2].b;w[2]=jx[2].a;
            l[3]=jx[3].a;w[3]=jx[3].b;
        }
        if( p==7 )
        {
            l[0]=jx[0].b;w[0]=jx[0].a;
            l[1]=jx[1].b;w[1]=jx[1].a;
            l[2]=jx[2].b;w[2]=jx[2].a;
            l[3]=jx[3].a;w[3]=jx[3].b;
        }
        if( p==8 )
        {
            l[0]=jx[0].a;w[0]=jx[0].b;
            l[1]=jx[1].a;w[1]=jx[1].b;
            l[2]=jx[2].a;w[2]=jx[2].b;
            l[3]=jx[3].b;w[3]=jx[3].a;
        }
        if( p==9 )
        {
            l[0]=jx[0].b;w[0]=jx[0].a;
            l[1]=jx[1].a;w[1]=jx[1].b;
            l[2]=jx[2].a;w[2]=jx[2].b;
            l[3]=jx[3].b;w[3]=jx[3].a;
        }
        if( p==10 )
        {
            l[0]=jx[0].a;w[0]=jx[0].b;
            l[1]=jx[1].b;w[1]=jx[1].a;
            l[2]=jx[2].a;w[2]=jx[2].b;
            l[3]=jx[3].b;w[3]=jx[3].a;
        }
        if( p==11 )
        {
            l[0]=jx[0].b;w[0]=jx[0].a;
            l[1]=jx[1].b;w[1]=jx[1].a;
            l[2]=jx[2].a;w[2]=jx[2].b;
            l[3]=jx[3].b;w[4]=jx[3].a;
        }
        if( p==12 )
        {
            l[0]=jx[0].a;w[0]=jx[0].b;
            l[1]=jx[1].a;w[1]=jx[1].b;
            l[2]=jx[2].b;w[2]=jx[2].a;
            l[3]=jx[3].b;w[3]=jx[3].a;
        }
        if( p==13 )
        {
            l[0]=jx[0].b;w[0]=jx[0].a;
            l[1]=jx[1].a;w[1]=jx[1].b;
            l[2]=jx[2].b;w[2]=jx[2].a;
            l[3]=jx[3].b;w[3]=jx[3].a;
        }
        if( p==14 )
        {
            l[0]=jx[0].a;w[0]=jx[0].b;
            l[1]=jx[1].b;w[1]=jx[1].a;
            l[2]=jx[2].b;w[2]=jx[2].a;
            l[3]=jx[3].b;w[3]=jx[3].a;
        }
        if( p==15 )
        {
            l[0]=jx[0].b;w[0]=jx[0].a;
            l[1]=jx[1].b;w[1]=jx[1].a;
            l[2]=jx[2].b;w[2]=jx[2].a;
            l[3]=jx[3].b;w[3]=jx[3].a;
        }
        //first
        ll=max( l[0],l[1],l[2],l[3] );
        ww=w[0]+w[1]+w[2]+w[3];
        t.v=ll*ww;
        t.a=min( ll,ww );
        t.b=max( ll,ww,0,0 );
        push( t );

        //second
        for( i=0;i<4;i++ )
        {
            ll=max( l[i],l[0]+l[1]+l[2]+l[3]-l[i] );
            ww=0;
            for( j=0;j<4;j++ )
            {
                if( j==i )
                    continue;
                ww=max( w[j],ww,0,0 );
            }
            ww=ww+w[i];
            t.v=ww*ll;
            t.a=min( ll,ww );
            t.b=ll+ww-t.a;
            push(t);
        }

        //third
        for( i=0;i<4;i++ )
        {
            for( j=0;j<4;j++ )
            {
                if( j==i )
                    continue;
                ll=max( l[0]+l[1]+l[2]+l[3]-l[i],l[i]+l[j],0,0 );
                ww=0;
                for( k=0;k<4;k++ )
                {
                    if( k==i||k==j )
                        continue;
                    ww=max( ww,w[k],0,0 );
                }
                ww=max( ww+w[i],w[j],0,0 );
                t.v=ll*ww;
                t.a=min( ll,ww );
                t.b=max( ll,ww,0,0 );
                push(t);
            }
        }
        //forth
        for( i=0;i<3;i++ )
        {
            for( j=i+1;j<4;j++ )
            {
                ll=max( l[0]+l[1]+l[2]+l[3]-l[i],l[0]+l[1]+l[2]+l[3]-l[j],0,0 );
                ww=0;
                for( k=0;k<4;k++ )
                {
                    if( k==i||k==j )
                        continue;
                    ww=max( ww,w[k],0,0 );
                }
                ww=max( ww,w[i]+w[j],0,0 );
                t.v=ll*ww;
                t.a=min( ll,ww );
                t.b=max( ll,ww,0,0 );
                push( t );
            }
        }
        //fifth
        for( i=0;i<4;i++ )
        {
            for( j=0;j<4;j++ )
            {
                if( j==i )
                    continue;
                for( k=0;k<4;k++ )
                {
                    if( k==i||k==j )
                        continue;
                    ll=max( l[i]+l[k],l[j]+l[6-i-k-j],0,0 );
                    if( l[k]>=l[j]+l[6-i-j-k] )
                        ww=max( w[i],w[j]+w[k],w[k]+w[6-i-j-k],0 );

                    if( l[k]>l[6-i-j-k]&&l[k]<l[j]+l[6-i-j-k] )
                        ww=max( w[i]+w[j],w[j]+w[k],w[k]+w[6-i-j-k],0 );

                    if( l[6-i-j-k]>l[k]&&l[6-i-j-k]<l[i]+l[k] )
                        ww=max( w[i]+w[j],w[i]+w[6-i-j-k],w[k]+w[6-i-j-k],0 );

                    if( l[6-i-j-k]>=l[i]+l[k] )
                        ww=max( w[j],w[i]+w[6-i-j-k],w[k]+w[6-i-j-k],0 );

                    if( l[6-i-j-k]==l[k] )
                        ww=max( w[i]+w[j],w[k]+w[6-i-k-j],0,0 );

                    t.v=ll*ww;
                    t.a=min( ll,ww );
                    t.b=max( ll,ww,0,0 );
                    push( t );
                }
            }
        }

    }


    sort( ans,ans+r+1,cmp );
    i=0;j=1;
    while( j<=r )
    {
        if( ans[i].a==ans[j].a )
            j++;
        else
        {
            i++;
            ans[i]=ans[j];
            j++;
        }
    }
    printf("%d\n",ans[r].v);
    r=i;
    for( i=0;i<=r;i++ )
        printf("%d %d\n",ans[i].a,ans[i].b);
    return 0;
}

从来没写过200+行的代码,这一次居然来了300+行,其实是我写麻烦了!

posted @ 2012-07-11 15:52  萧若离  阅读(421)  评论(0编辑  收藏  举报