HDU 4001 To Miss Our Children Time

题意: 有n 个砖块,有不同的长宽高,分为三种类型,要求是:

       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.

       问最多可以堆成多高的积木

分析:之间按题意DP

        先排序,排序先 按len 再按 wi 再按 d

        dp[i]  表示前 i 块砖的最大高度.

 

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define clr(x)memset(x,0,sizeof(x))
long long max(long long a,long long b)
{
    return a>b?a:b;
}
struct node
{
    int wi, le, th, d;
}a[1005];
bool cmp(node a,node b)
{
    if(a.le != b.le)
    {
        return a.le < b.le;
    }
    if(a.wi != b.wi)
    {
        return a.wi < b.wi;
    }
    return a.d > b.d;
}
long long dp[1005];
int main()
{
    int n, i, j;
    while (scanf("%d",&n),n)
    {
        //a[0].le = a[0].wi = 0;
        for(i=1; i<=n; i++){
            scanf("%d %d %d %d",&a[i].le,&a[i].wi,&a[i].th,&a[i].d);
            if(a[i].le >  a[i].wi)
            {
                int tmp = a[i].le;
                a[i].le = a[i].wi;
                a[i].wi = tmp;
            }
        }
        sort(a+1,a+n+1,cmp);
        clr(dp);
        long long res = a[1].th;
        for(i=1; i<=n; i++)
        {
            dp[i] = a[i].th;
            res = max(res,dp[i]);
        }
        for(i=1; i<=n; i++)
        {
            //dp[i] = dp[i-1];
            for(j=1; j< i; j++)
            {
                if(a[i].d == 0)
                {
                    if(a[j].le <= a[i].le && a[j].wi <= a[i].wi)
                        dp[i] = max(dp[i],dp[j]+a[i].th);
                }
                else if(a[i].d == 1)
                {
                    if(a[j].le <= a[i].le && a[j].wi <= a[i].wi 
                    && (a[j].le < a[i].le || a[j].wi < a[i].wi))
                        dp[i] = max(dp[i],dp[j]+a[i].th);
//                    printf("%d__%d\n",i,dp[i]);
                }
                else if(a[i].d == 2)
                {
                    if(a[j].le < a[i].le && a[j].wi < a[i].wi)
                        dp[i] = max(dp[i],dp[j]+a[i].th);
                }
            }
            res = max(res,dp[i]);
        }
        printf("%I64d\n",res);
    }
    return 0;
}

 

 

 

posted @ 2012-11-03 00:19  'wind  阅读(353)  评论(0编辑  收藏  举报