动态规划 杭电4001 大连赛区网络赛水题
2012-01-03 19:50 javaspring 阅读(165) 评论(0) 编辑 收藏 举报看别人说这是打联赛区网络赛的水题,,水题,,,结果水了几个小时,有木有!!!!!!水不水!!!!!!!由于下午刚做了一道类似的题,所以做这道题的时候就感觉简单了一点。。刚开始wa了几次,主要是排序没排好。个人感觉此类题目最主要的还是排好序,能排好序,这道题就基本上解决了。具体来说这道题,就是按长、宽排序,当长和宽都相等时,则按类型序号排序。因为由题意知,类型序号大的条件更严格,因此,在满足条件的前提下,应该先放类型序号大的。最后在遍历一遍,找最大值即可。需要注意的是数据类型,用_int64即可。
题目:
To Miss Our Children Time
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 2375 Accepted Submission(s): 636
Problem Description
Do you remember our children time? When we are children, we are interesting in almost everything around ourselves. A little thing or a simple game will brings us lots of happy time! LLL is a nostalgic boy, now he grows up. In the dead of night, he often misses
something, including a simple game which brings him much happy when he was child. Here are the game rules: There lies many blocks on the ground, little LLL wants build "Skyscraper" using these blocks. There are three kinds of blocks signed by an integer d.
We describe each block's shape is Cuboid using four integers ai, bi, ci, di. ai, bi are two edges of the block one of them is length the other is width. ci is
thickness of the block. We know that the ci must be vertical with earth ground. di describe the kind of the block. When di = 0 the block's length and width must be more or equal to the block's length and width which lies under the block. When di = 1 the block's length and width must be more or equal to the block's length which lies under the block and width and the block's area must be more than the block's area which lies under the block. When di = 2 the block length and width must be more than the block's length and width which lies under the block. Here are some blocks. Can you know what's the highest "Skyscraper" can be build using these blocks?
thickness of the block. We know that the ci must be vertical with earth ground. di describe the kind of the block. When di = 0 the block's length and width must be more or equal to the block's length and width which lies under the block. When di = 1 the block's length and width must be more or equal to the block's length which lies under the block and width and the block's area must be more than the block's area which lies under the block. When di = 2 the block length and width must be more than the block's length and width which lies under the block. Here are some blocks. Can you know what's the highest "Skyscraper" can be build using these blocks?
Input
The input has many test cases.
For each test case the first line is a integer n ( 0< n <= 1000) , the number of blocks.
From the second to the n+1'th lines , each line describing the i‐1'th block's a,b,c,d (1 =< ai,bi,ci <= 10^8 , d = 0 or 1 or 2).
The input end with n = 0.
For each test case the first line is a integer n ( 0< n <= 1000) , the number of blocks.
From the second to the n+1'th lines , each line describing the i‐1'th block's a,b,c,d (1 =< ai,bi,ci <= 10^8 , d = 0 or 1 or 2).
The input end with n = 0.
Output
Output a line contains a integer describing the highest "Skyscraper"'s height using the n blocks.
Sample Input
3 10 10 12 0 10 10 12 1 10 10 11 2 2 10 10 11 1 10 10 11 1 0
Sample Output
24 11
#include <iostream> #include <cstdio> #include <algorithm> #include <string> using namespace std; struct block{ int num; _int64 length; _int64 width; _int64 height; _int64 area; }aa[1005]; bool cmp(block a,block b){ if(a.length==b.length){ if(a.width==b.width) return a.num>b.num; else return a.width<b.width; } return a.length<b.length; } int main(){ // freopen("4.txt","r",stdin); int n; while(scanf("%d",&n)&&n){ _int64 a,b,dp[1005]; for(int i=0;i<n;++i){ scanf("%I64d%I64d%I64d%d",&a,&b,&aa[i].height,&aa[i].num); aa[i].length=a>b?a:b; aa[i].width=a<b?a:b; aa[i].area=aa[i].length*aa[i].width; } sort(aa,aa+n,cmp); for(int i=0;i<n;++i) dp[i]=aa[i].height; _int64 xmax=0; for(int i=1;i<n;++i){ xmax=0; if(aa[i].num==2){ for(int j=0;j<i;++j){ if(aa[j].length<aa[i].length&&aa[j].width<aa[i].width&&dp[j]>xmax) xmax=dp[j]; } } else if(aa[i].num==1){ for(int j=0;j<i;++j){ if(aa[j].length<=aa[i].length&&aa[j].width<=aa[i].width&&aa[j].area<aa[i].area&&dp[j]>xmax) xmax=dp[j]; } } else{ for(int j=0;j<i;++j){ if(aa[j].length<=aa[i].length&&aa[j].width<=aa[i].width&&dp[j]>xmax) xmax=dp[j]; } } //printf("xmax==%d\n",xmax); dp[i]=aa[i].height+xmax; } xmax=0; for(int i=0;i<n;++i) if(dp[i]>xmax) xmax=dp[i]; printf("%I64d\n",xmax); } return 0; }