【HDU 4001】 To Miss Our Children Time(DP)
To Miss Our Children Time
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 4452 Accepted Submission(s): 1193
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
Source
Recommend
a,b表示长与宽 长宽可以对调
c表示高度
d == 0 表示在垒积木的过程中 长与宽必须大于等于下面的积木的长与宽
d == 1 表示在垒积木的过程中 长与宽必须大于等于下面的积木的长于宽 并且面积必须大于下面的积木的面积(其实第一个条件已经保证了面积大于等于下面的积木 如果要求面积大于 就说明长与宽最多只有一项可以相等)
d == 2 表示在垒积木的过程中 长于宽必须大于下面的积木的长于宽
这样判断条件知道了 题目要求求出最高能垒出的高度
可以设dp数组表示第i个积木为顶的最大高度
这样提前对积木长宽排个序 可以保证不会出现前面的积木可以垒在后面的积木上的情况 就是保证不会出现第i个积木可以垒在第j个积木上(i < j)
从第一个积木往后遍历 每次看看能不能给之前垒出的高度增高 这样既保证了每个积木最多垒一次 又保证了答案的正确性
代码如下:
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout)
using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 1e9+7;
const double eps = 1e-8;
struct Object
{
int a,b,c,d;
bool operator < (const struct Object ob)const
{
if(a != ob.a) return a < ob.a;
if(b != ob.b) return b < ob.b;
return d > ob.d;
}
};
Object obj[1001];
LL dp[1001];
int main()
{
//fread();
//fwrite();
int n;
while(~scanf("%d",&n) && n)
{
for(int i = 0; i < n; ++i)
{
scanf("%d %d %d %d",&obj[i].a,&obj[i].b,&obj[i].c,&obj[i].d);
if(obj[i].a > obj[i].b) swap(obj[i].a,obj[i].b);
}
sort(obj,obj+n);
for(int i = 0; i < n; ++i)
{
dp[i] = obj[i].c;
for(int j = 0; j < i; ++j)
{
if(obj[i].d == 0)
{
if(obj[i].a >= obj[j].a && obj[i].b >= obj[j].b)
dp[i] = max(dp[i],dp[j]+obj[i].c);
}
else if(obj[i].d == 1)
{
if((obj[i].a > obj[j].a && obj[i].b >= obj[j].b) || (obj[i].a >= obj[j].a && obj[i].b > obj[j].b))
dp[i] = max(dp[i],dp[j]+obj[i].c);
}
else if(obj[i].a > obj[j].a && obj[i].b > obj[j].b)
dp[i] = max(dp[i],dp[j]+obj[i].c);
}
}
LL ans = 0;
for(int i = 0; i < n; ++i)
{
ans = max(ans,dp[i]);
}
printf("%lld\n",ans);
}
return 0;
}