1. 程式人生 > >poj 4001 To Miss Our Children Time

poj 4001 To Miss Our Children Time

des 遞推 ++ urn tro ostream clas element type

To Miss Our Children Time

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 4740 Accepted Submission(s): 1319


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?

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.

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 題意:堆長方體,一共三種類型的長方體,0型號的長方體必須堆在長和寬都小於等於本身的長方體之上,1型號的長方體必須堆在長或者寬小於等於它本身的長方體之上,也就是說在其下面的長方體必有長或寬中一者是小於該長方體的。2型號的長方體必須堆在長和寬都完全小於其本身的長方體之上。滿足上述條件,求能堆多高。 思路:先排好序,讓長和寬數值比較大的長方體盡量先堆。這樣排在隊列前面的長方體肯定比排在後面的長方體先開始堆,遞推,可以利用dp。
#define
_CRT_SECURE_NO_DEPRECATE #include<iostream> #include<algorithm> #include<string> #include<cmath> #include<queue> #include<set> #include<map> using namespace std; typedef long long ll; const int N_MAX = 1000 + 5; ll dp[N_MAX]; int n; struct Cube { ll w, l,h; int id; bool operator <(const Cube&b) { if (this->l != b.l)return this->l < b.l; else if(this->w!=b.w) return this->w < b.w; else return this->id > b.id; } }cube[N_MAX]; bool judge(const Cube& a,const Cube& b) {//b要堆在a上面 if (b.id == 0)return b.l >= a.l&&b.w >= a.w; if (b.id == 1)return (b.l >= a.l&&b.w > a.w) || (b.l > a.l&&b.w >= a.w); return b.l > a.l&&b.w > a.w; } void Dp() { for (int i = 0; i < n;i++) { dp[i] = cube[i].h; } for (int i = 1; i < n;i++) {//每次cube[i]要堆在最上面 for (int j = 0; j < i;j++) { if (judge(cube[j], cube[i]))dp[i] = max(dp[i], dp[j] + cube[i].h); } } ll sum = *max_element(dp, dp + n); printf("%I64d\n",sum); } int main() { while (scanf("%d",&n)&&n) { for (int i = 0; i < n;i++) { scanf("%I64d%I64d%I64d%d",&cube[i].l,&cube[i].w,&cube[i].h,&cube[i].id); if (cube[i].l < cube[i].w)swap(cube[i].l, cube[i].w); } sort(cube, cube + n); Dp(); } return 0; }

poj 4001 To Miss Our Children Time