您的位置:首页 > 其它

USCAO section 1.4 Packing Rectangles

2012-07-20 16:48 218 查看
PackingRectangles
IOI95


ThesixbasiclayoutsoffourrectanglesFourrectanglesaregiven.Findthesmallestenclosing(new)rectangleintowhichthesefourmaybefittedwithoutoverlapping.Bysmallestrectangle,wemeantheonewiththesmallestarea.

Allfourrectanglesshouldhavetheirsidesparalleltothecorrespondingsidesoftheenclosingrectangle.Figure1showssixwaystofitfourrectanglestogether.Thesesixaretheonlypossiblebasiclayouts,sinceanyotherlayoutcanbeobtainedfromabasiclayoutbyrotationorreflection.Rectanglesmayberotated90degreesduringpacking.

Theremayexistseveraldifferentenclosingrectanglesfulfillingtherequirements,allwiththesamearea.Youmustproduceallsuchenclosingrectangles.

PROGRAMNAME:packrec

INPUTFORMAT

Fourlines,eachcontainingtwopositivespace-separatedintegersthatrepresentthelengthsofarectangle'stwosides.Eachsideofarectangleisatleast1andatmost50.

SAMPLEINPUT(filepackrec.in)

[code]12 23 34 45

OUTPUTFORMAT

Theoutputfilecontainsonelinemorethanthenumberofsolutions.Thefirstlinecontainsasingleinteger:theminimumareaoftheenclosingrectangles.Eachofthefollowinglinescontainsonesolutiondescribedbytwonumberspandqwithp<=q.Theselinesmustbesortedinascendingorderofp,andmustallbedifferent.

SAMPLEOUTPUT(filepackrec.out)

40
410
58


有点恶心,要用全排列,可惜我还不会,数据小,就用几个for来搞定了,

需要注意的是最后一种情况,小心点就成了。

[/code]


/*
ID:nealgav1
PROG:packrec
LANG:C++
*/
#include<cstdio>
#include<algorithm>
#include<stdlib.h>
#defineN123456
usingnamespacestd;
structA
{
intw;
inth;
};
structB
{
Asq[5];
intare;
intw;
inth;
};
Barea
;
Acun[10];
Aans
;
boolcmp(Aa,Ab)
{
returna.h<b.h;
}
intmain()
{
freopen("packrec.in","r",stdin);
freopen("packrec.out","w",stdout);
while(scanf("%d%d",&cun[0].w,&cun[0].h)!=EOF)
{
for(inti=1;i<4;i++)
scanf("%d%d",&cun[i].w,&cun[i].h);
intkk=0;
intmid;
for(inti=0;i<4;i++)
{
for(intj=0;j<4;j++)
{
if(j==i)
continue;
for(intk=0;k<4;k++)
{if(k==i||k==j)
continue;
for(intl=0;l<4;l++)
{
if(l==i||l==j||l==k)
continue;
for(intii=0;ii<2;ii++)
{if(ii)
{
mid=cun[0].h;
cun[0].h=cun[0].w;
cun[0].w=mid;
}
for(intjj=0;jj<2;jj++)
{
if(jj)
{
mid=cun[1].h;
cun[1].h=cun[1].w;
cun[1].w=mid;
}
for(intkkk=0;kkk<2;kkk++)
{
if(kkk)
{
mid=cun[2].h;
cun[2].h=cun[2].w;
cun[2].w=mid;

}
for(intll=0;ll<2;ll++)
{
if(ll)
{
mid=cun[3].h;
cun[3].h=cun[3].w;
cun[3].w=mid;
}
area[kk].sq[0].w=cun[i].w;
area[kk].sq[0].h=cun[i].h;
area[kk].sq[1].w=cun[j].w;
area[kk].sq[1].h=cun[j].h;
area[kk].sq[2].w=cun[k].w;
area[kk].sq[2].h=cun[k].h;
area[kk].sq[3].w=cun[l].w;
area[kk].sq[3].h=cun[l].h;
kk++;
}
}
}
}

}
}
}
}
intg=0;intareare=9999999;
for(inti=0;i<kk;i++)
{
//1
intll;
intrr;
ll=area[i].sq[0].h+area[i].sq[1].h+area[i].sq[2].h+area[i].sq[3].h;
rr=max(area[i].sq[0].w,max(area[i].sq[1].w,max(area[i].sq[2].w,area[i].sq[3].w)));
if(areare>ll*rr)
{
areare=ll*rr;
g=0;
if(ll<rr)
{
ans[g].h=ll;
ans[g].w=rr;
}
else
{
ans[g].h=rr;
ans[g].w=ll;
}

}
elseif(areare==ll*rr)
{
g++;
if(ll<rr)
{
ans[g].h=ll;
ans[g].w=rr;
}
else
{
ans[g].h=rr;
ans[g].w=ll;
}
}
//2
ll=max(area[i].sq[0].h+area[i].sq[1].h+area[i].sq[2].h,area[i].sq[3].h);
rr=area[i].sq[3].w+max(area[i].sq[0].w,max(area[i].sq[1].w,area[i].sq[2].w));
if(areare>ll*rr)
{
areare=ll*rr;
g=0;
if(ll<rr)
{
ans[g].h=ll;
ans[g].w=rr;
}
else
{
ans[g].h=rr;
ans[g].w=ll;
}
}
elseif(areare==ll*rr)
{
g++;
if(ll<rr)
{
ans[g].h=ll;
ans[g].w=rr;
}
else
{
ans[g].h=rr;
ans[g].w=ll;
}
}
ll=max(area[i].sq[0].h+area[i].sq[1].h+area[i].sq[2].h,area[i].sq[2].h+area[i].sq[3].h);
rr=max(area[i].sq[2].w,max(area[i].sq[0].w,area[i].sq[1].w)+area[i].sq[3].w);
if(areare>ll*rr)
{
areare=ll*rr;
g=0;
if(ll<rr)
{
ans[g].h=ll;
ans[g].w=rr;
}
else
{
ans[g].h=rr;
ans[g].w=ll;
}
}
elseif(areare==ll*rr)
{
g++;
if(ll<rr)
{
ans[g].h=ll;
ans[g].w=rr;
}
else
{
ans[g].h=rr;
ans[g].w=ll;
}
}
ll=max(area[i].sq[0].h,max(area[i].sq[3].h,area[i].sq[1].h+area[i].sq[2].h));
rr=max(area[i].sq[1].w,area[i].sq[2].w)+area[i].sq[0].w+area[i].sq[3].w;
if(areare>ll*rr)
{
areare=ll*rr;
g=0;
if(ll<rr)
{
ans[g].h=ll;
ans[g].w=rr;
}
else
{
ans[g].h=rr;
ans[g].w=ll;
}
}
elseif(areare==ll*rr)
{
g++;
if(ll<rr)
{
ans[g].h=ll;
ans[g].w=rr;
}
else
{
ans[g].h=rr;
ans[g].w=ll;
}
}
rr=max(area[i].sq[0].w,area[i].sq[1].w)+max(area[i].sq[2].w,area[i].sq[3].w);
ll=max(area[i].sq[0].h+area[i].sq[1].h,area[i].sq[2].h+area[i].sq[3].h);
if(area[i].sq[0].w>=area[i].sq[1].w&&area[i].sq[1].h>=area[i].sq[3].h)
rr=max(area[i].sq[0].w+area[i].sq[2].w,area[i].sq[1].w+area[i].sq[3].w);
if(areare>ll*rr)
{
areare=ll*rr;
g=0;
if(ll<rr)
{
ans[g].h=ll;
ans[g].w=rr;
}
else
{
ans[g].h=rr;
ans[g].w=ll;
}
}
elseif(areare==ll*rr)
{
g++;
if(ll<rr)
{
ans[g].h=ll;
ans[g].w=rr;
}
else
{
ans[g].h=rr;
ans[g].w=ll;
}
}
}
sort(ans,ans+g+1,cmp);
printf("%d\n",areare);
ans[g+1].h=-2;
for(intj=0;j<=g;j++)
{if(ans[j].h==ans[j+1].h)
continue;
printf("%d%d\n",ans[j].h,ans[j].w);
}
}
}

[/code]


PackingRectangles
RussCoxThisprogramisstraightforward,butabitlongduetothegeometryinvolved.

Thereare24permutationsofthe4rectangles,andforeachpermutation,16differentwaystoorientthem.Wegenerateallsuchorientationsofpermutations,andputtheblockstogetherineachofthe6differentways,recordingthesmallestrectangleswefind.

[code]#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>

typedefstructRectRect;
structRect{
intwid;
intht;
};

Rect
rotate(Rectr)
{
Rectnr;

nr.wid=r.ht;
nr.ht=r.wid;
returnnr;
}

int
max(inta,intb)
{
returna>b?a:b;
}

int
min(inta,intb)
{
returna<b?a:b;
}

inttot;
intbestarea;
intbestht[101];

void
record(Rectr)
{
inti;

if(r.wid*r.ht<tot)
*(long*)0=0;

if(r.wid*r.ht<bestarea||bestarea==0){
bestarea=r.wid*r.ht;
for(i=0;i<=100;i++)
bestht[i]=0;
}
if(r.wid*r.ht==bestarea)
bestht[min(r.wid,r.ht)]=1;
}

void
check(Rect*r)
{
Rectbig;
inti;

/*schema1:alllinedupnexttoeachother*/
big.wid=0;
big.ht=0;
for(i=0;i<4;i++){
big.wid+=r[i].wid;
big.ht=max(big.ht,r[i].ht);
}
record(big);

/*schema2:firstthreelinedup,fourthonbottom*/
big.wid=0;
big.ht=0;
for(i=0;i<3;i++){
big.wid+=r[i].wid;
big.ht=max(big.ht,r[i].ht);
}
big.ht+=r[3].ht;
big.wid=max(big.wid,r[3].wid);
record(big);

/*schema3:firsttwolinedup,thirdunderthem,fourthtoside*/
big.wid=r[0].wid+r[1].wid;
big.ht=max(r[0].ht,r[1].ht);
big.ht+=r[2].ht;
big.wid=max(big.wid,r[2].wid);
big.wid+=r[3].wid;
big.ht=max(big.ht,r[3].ht);
record(big);

/*schema4,5:firsttworectangleslinedup,nexttwostacked*/
big.wid=r[0].wid+r[1].wid;
big.ht=max(r[0].ht,r[1].ht);
big.wid+=max(r[2].wid,r[3].wid);
big.ht=max(big.ht,r[2].ht+r[3].ht);
record(big);

/*
*schema6:firsttwopressednexttoeachother,nexttwoontop,like:
*23
*01
*/
big.ht=max(r[0].ht+r[2].ht,r[1].ht+r[3].ht);
big.wid=r[0].wid+r[1].wid;

/*do2and1touch?*/
if(r[0].ht<r[1].ht)
big.wid=max(big.wid,r[2].wid+r[1].wid);
/*do2and3touch?*/
if(r[0].ht+r[2].ht>r[1].ht)
big.wid=max(big.wid,r[2].wid+r[3].wid);
/*do0and3touch?*/
if(r[1].ht<r[0].ht)
big.wid=max(big.wid,r[0].wid+r[3].wid);

/*maybe2or3sitsbyitself*/
big.wid=max(big.wid,r[2].wid);
big.wid=max(big.wid,r[3].wid);
record(big);
}

void
checkrotate(Rect*r,intn)
{
if(n==4){
check(r);
return;
}

checkrotate(r,n+1);
r
=rotate(r
);
checkrotate(r,n+1);
r
=rotate(r
);
}

void
checkpermute(Rect*r,intn)
{
Rectt;
inti;

if(n==4)
checkrotate(r,0);

for(i=n;i<4;i++){
t=r
,r
=r[i],r[i]=t;/*swapr[i],r
*/
checkpermute(r,n+1);
t=r
,r
=r[i],r[i]=t;/*swapr[i],r
*/
}
}

void
main(void)
{
FILE*fin,*fout;
Rectr[4];
inti;

fin=fopen("packrec.in","r");
fout=fopen("packrec.out","w");
assert(fin!=NULL&&fout!=NULL);

for(i=0;i<4;i++)
fscanf(fin,"%d%d",&r[i].wid,&r[i].ht);

tot=(r[0].wid*r[0].ht+r[1].wid*r[1].ht+r[2].wid*r[2].ht+r[3].wid*r[3].ht);

checkpermute(r,0);
fprintf(fout,"%d\n",bestarea);
for(i=0;i<=100;i++)
if(bestht[i])
fprintf(fout,"%d%d\n",i,bestarea/i);
exit(0);
}


[/code]

USER:NealGavinGavin[nealgav1]
TASK:packrec
LANG:C++

Compiling...
Compile:OK

Executing...
Test1:TESTOK[0.000secs,10584KB]
Test2:TESTOK[0.000secs,10584KB]
Test3:TESTOK[0.000secs,10584KB]
Test4:TESTOK[0.000secs,10584KB]
Test5:TESTOK[0.000secs,10584KB]
Test6:TESTOK[0.000secs,10584KB]
Test7:TESTOK[0.000secs,10584KB]
Test8:TESTOK[0.000secs,10584KB]
Test9:TESTOK[0.000secs,10584KB]
Test10:TESTOK[0.000secs,10584KB]
Test11:TESTOK[0.000secs,10584KB]
Test12:TESTOK[0.000secs,10584KB]
Test13:TESTOK[0.000secs,10584KB]
Test14:TESTOK[0.000secs,10584KB]
Test15:TESTOK[0.000secs,10584KB]
Test16:TESTOK[0.000secs,10584KB]
Test17:TESTOK[0.000secs,10584KB]
Test18:TESTOK[0.000secs,10584KB]
Test19:TESTOK[0.000secs,10584KB]
Test20:TESTOK[0.000secs,10584KB]
Test21:TESTOK[0.000secs,10584KB]

AlltestsOK.
Yourprogram('packrec')producedallcorrectanswers!Thisisyour
submission#2forthisproblem.Congratulations!


Herearethetestdatainputs:

-------test1----
12
23
34
45
-------test2----
2020
2020
2020
2020
-------test3----
45
54
45
161
-------test4----
45
25
52
210
-------test5----
1218
46
217
193
-------test6----
1010
55
1515
2020
-------test7----
11
120
120
2020
-------test8----
58
312
154
1410
-------test9----
45
56
64
45
-------test10----
15
510
1015
1520
-------test11----
34
85
71
45
-------test12----
1711
1620
46
1319
-------test13----
42
26
23
58
-------test14----
12
23
34
45
-------test15----
48
812
1216
1620
-------test16----
35
13
24
25
-------test17----
43
44
63
55
-------test18----
4950
4950
4950
4950
-------test19----
1050
4530
2838
3620
-------test20----
5049
4948
4847
4746
-------test21----
5049
4847
4645
4544

Keepupthegoodwork!


Thanksforyoursubmission!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: