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
(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
.
Thislineisfollowedbyklines,onelineperboxwiththenmeasurementsofeachboxononelineseparatedbyoneormorespaces.The
lineinthesequence(
)givesthemeasurementsforthe
box.
Theremaybeseveralboxsequencesintheinputfile.Yourprogramshouldprocessallofthemanddetermine,foreachsequence,whichofthekboxesdeterminethelongestnestingstringandthelengthofthatnestingstring(thenumberofboxesinthestring).
Inthisproblemthemaximumdimensionalityis10andtheminimumdimensionalityis1.Themaximumnumberofboxesinasequenceis30.
Theboxesshouldbenumberedaccordingtotheorderinwhichtheyappearedintheinputfile(firstboxisbox1,etc.).
Ifthereismorethanonelongestnestingstringthenanyoneofthemcanbeoutput.
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; }
相关文章推荐
- linux vim编辑器
- XML解析
- Storyboard代码跳转的几种方法
- IT服务管理国际标准体系学习笔记-1
- 庖丁解牛分词器---源码下载---错误问题解决
- HTML5规范的本地存储
- Screen tearing
- MVC中发生System.Data.Entity.Validation.DbEntityValidationException验证异常的解决方法
- LAMP 3.0 mysql配置讲解
- linux下svn命令使用大全
- Granafa http api使用
- OC-035.协议
- 黑马程序员——多线程笔记
- phpStudy for Linux (lnmp+lamp一键安装包)
- C#_ArrayList的使用方法
- Android之Fragment学习(一)
- hdu 1757 A Simple Math Problem(矩阵快速幂乘法)
- 创建一个目录的软连接ln -s和打印当前目录pwd的一个知识点
- Hibernate实体关系映射:单向主键一对一关联
- Hibernate框架学习之一:Hello World程序