hdu 4001 (2011ACM/ICPC大连网络赛)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4001

题意不多解释,说说思路:按照长和宽排序,排序后,前面的方块一定不能叠放在后面的方块上,而后面的方块有可能放在前面的方块上。然后DP找出最优解。

详见代码:
#include<iostream>
#include<algorithm>
using namespace std;

struct node{
    int a,b,d;
    __int64 c;
}block[1100];
int cmp(const void *a,const void *b)
{   
    node t1,t2;
    t1 = *(node *)a;
    t2 = *(node *)b;
    if(t1.a != t2.a)return t1.a - t2.a; 
    if(t1.b != t2.b)return t1.b - t2.b;  
    return t2.d - t1.d;
}

__int64 f[1100];

int main()
{
    int i,j,n,k;
    __int64 ans;
    while(scanf("%d",&n) && n)
    {
        ans = 0;
        for(i = 0;i < n;i++)
        {
            scanf("%d%d%I64d%d",&block[i].a,&block[i].b,&block[i].c,&block[i].d);
            if(block[i].a <block[i].b)
                swap(block[i].a,block[i].b);
        }
        qsort(&block[0],n,sizeof(block[0]),cmp);
        for(i = 0;i < n;i++)
        {
            f[i] = block[i].c;
            for(j = 0;j < i;j++)
            {
                if(block[i].d == 0 && block[i].a >= block[j].a && block[i].b >= block[j].b && f[j] + block[i].c > f[i])
                    f[i] = f[j] + block[i].c;
                else if(block[i].d == 1 && block[i].a >= block[j].a && block[i].b >= block[j].b && (block[i].a > block[j].a || block[i].b > block[j].b) && f[j] + block[i].c > f[i])
                    f[i] = f[j] + block[i].c;
                else if(block[i].d == 2 && block[i].a > block[j].a && block[i].b > block[j].b && f[j] + block[i].c > f[i])
                    f[i] = f[j] + block[i].c;
            }
            if(f[i] > ans)
                ans = f[i];
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(hdu 4001 (2011ACM/ICPC大连网络赛))