您的位置:首页 > 其它

UVa 103 - Stacking Boxes(dp求解)

2015-12-12 16:42 405 查看
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=39

StackingBoxes

Background

SomeconceptsinMathematicsandComputerSciencearesimpleinoneortwodimensionsbutbecomemorecomplexwhenextendedtoarbitrarydimensions.Considersolvingdifferentialequationsinseveraldimensionsandanalyzingthetopologyofann-dimensionalhypercube.Theformerismuchmorecomplicatedthanitsonedimensionalrelativewhilethelatterbearsaremarkableresemblancetoits``lower-class''cousin.

TheProblem

Considerann-dimensional``box''givenbyitsdimensions.Intwodimensionsthebox(2,3)mightrepresentaboxwithlength2unitsandwidth3units.Inthreedimensionsthebox(4,8,9)canrepresentabox

(length,width,andheight).In6dimensionsitis,perhaps,unclearwhatthebox(4,5,6,7,8,9)represents;butwecananalyzepropertiesoftheboxsuchasthesumofitsdimensions.

Inthisproblemyouwillanalyzeapropertyofagroupofn-dimensionalboxes.Youaretodeterminethelongestnestingstringofboxes,thatisasequenceofboxes

suchthateachbox

nestsinbox

(

.

AboxD=(

)nestsinaboxE=(

)ifthereissomerearrangementofthe

suchthatwhenrearrangedeachdimensionislessthanthecorrespondingdimensioninboxE.ThislooselycorrespondstoturningboxDtoseeifitwillfitinboxE.However,sinceanyrearrangementsuffices,boxDcanbecontorted,notjustturned(seeexamplesbelow).

Forexample,theboxD=(2,6)nestsintheboxE=(7,3)sinceDcanberearrangedas(6,2)sothateachdimensionislessthanthecorrespondingdimensioninE.TheboxD=(9,5,7,3)doesNOTnestintheboxE=(2,10,6,8)sincenorearrangementofDresultsinaboxthatsatisfiesthenestingproperty,butF=(9,5,7,1)doesnestinboxEsinceFcanberearrangedas(1,9,5,7)whichnestsinE.

Formally,wedefinenestingasfollows:boxD=(

)nestsinboxE=(

)ifthereisapermutation

of

suchthat(

)``fits''in(

)i.e.,if

forall

.

TheInput

Theinputconsistsofaseriesofboxsequences.Eachboxsequencebeginswithalineconsistingofthethenumberofboxeskinthesequencefollowedbythedimensionalityoftheboxes,n(onthesameline.)

Thislineisfollowedbyklines,onelineperboxwiththenmeasurementsofeachboxononelineseparatedbyoneormorespaces.The

lineinthesequence(

)givesthemeasurementsforthe

box.

Theremaybeseveralboxsequencesintheinputfile.Yourprogramshouldprocessallofthemanddetermine,foreachsequence,whichofthekboxesdeterminethelongestnestingstringandthelengthofthatnestingstring(thenumberofboxesinthestring).

Inthisproblemthemaximumdimensionalityis10andtheminimumdimensionalityis1.Themaximumnumberofboxesinasequenceis30.

TheOutput

Foreachboxsequenceintheinputfile,outputthelengthofthelongestnestingstringononelinefollowedonthenextlinebyalistoftheboxesthatcomprisethisstringinorder.The``smallest''or``innermost''boxofthenestingstringshouldbelistedfirst,thenextbox(ifthereisone)shouldbelistedsecond,etc.

Theboxesshouldbenumberedaccordingtotheorderinwhichtheyappearedintheinputfile(firstboxisbox1,etc.).

Ifthereismorethanonelongestnestingstringthenanyoneofthemcanbeoutput.

SampleInput

52
37
810
52
911
2118
86
522013010
231579113
40503424144
91011121314
3141882717
443213194119
123456
80374718219


SampleOutput

5
31245
4
7256

解题思路:

题目意思:给定n个m维的矩形,问我们能够嵌套的矩形最多有几个,输出个数和嵌套的矩形编号。
代码:


#include<bits/stdc++.h>
#defineinf0x7fffffff
usingnamespacestd;
typedeflonglongLL;

intk,n;
intdp[33],pre[33];
structnode
{
intan[13];
intid;
friendbooloperator<(nodea,nodeb)
{
for(inti=0;i<n;i++)
{
if(a.an[i]!=b.an[i])returna.an[i]<b.an[i];
}
}
}arr[33];

voidprintOut(intu)
{
if(pre[u]!=-1)printOut(pre[u]);
if(pre[u]==-1)printf("%d",arr[u].id+1);
elseprintf("%d",arr[u].id+1);
}

intmain()
{
while(scanf("%d%d",&k,&n)!=EOF)
{
memset(dp,0,sizeof(dp));
memset(pre,-1,sizeof(pre));
for(inti=0;i<k;i++)
{
for(intj=0;j<n;j++)
scanf("%d",&arr[i].an[j]);
arr[i].id=i;
sort(arr[i].an,arr[i].an+n);
}
sort(arr,arr+k);
for(inti=0;i<k;i++)
{
inttemp=0;
for(intj=0;j<i;j++)
{
intflag=0;
for(intu=0;u<n;u++)
if(arr[i].an[u]<=arr[j].an[u]){flag=1;break;}
if(!flag&&dp[j]>temp)
{
temp=dp[j];
pre[i]=j;
}
}
dp[i]=temp+1;
}
intmaxlen=-1,num=0;
for(inti=0;i<k;i++)
{
if(dp[i]>maxlen)
{
maxlen=dp[i];
num=i;
}
}
printf("%d\n",maxlen);
printOut(num);
printf("\n");
}
return0;
}



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