HDU 1069 DP

来源:互联网 发布:mac os x mavericks 编辑:程序博客网 时间:2024/06/05 16:17

题解:首先我们要发现,这个题每一种长方体其实最多也就使用一次,因为题目说下面的要严格大于上面的低和宽,这样的话我们就可以把一个长方体拆成三个,分别作低和宽,然后我们按照底面的大小之类的东西排个序 (注意这个地方必须排序,因为这里不同于背包,选了前面对后面没有影响这个题目是有影响的)所有两个的话你会怎么选 非常关键 然后就是dp了

dp(i) 表示前 i 个并且选择第 i 个的最大高度dp(i) = dp (j) + high (i); 其中 j < i 并且i 能放在 j 上面
#include <iostream>#include <cstring>#include <algorithm>#include <cmath>#include <cstdio>using namespace std;const int maxn = 305;struct node {    int len,wid;    int high;}arr[maxn];int dp[maxn] = {0};int cnt = 0;void build (int a,int b,int c) {    if (a < b) swap (a,b);    arr[cnt].len = a,arr[cnt].wid = b,arr[cnt].high = c;    cnt ++;}bool cmp (const node a,const node b) {    if (a.len == b.len) return a.wid > b.wid;    return a.len > b.len;}int main () {    ios_base :: sync_with_stdio(false);    int n;    int cas = 1;    while (cin >> n && n) {        cnt = 0;        for (int i = 1;i <= n; ++ i) {            int a,b,c;            cin >> a >> b >> c;            build(a, b, c);            build(a, c, b);            build(b, c, a);        }        sort (arr,arr + cnt,cmp);        memset (dp,0,sizeof(0));        dp[0] = 0;        int ans = 0;        for (int i = 0;i < cnt; ++ i) {            dp[i] = arr[i].high;            ans = max (ans,dp[i]);            for (int j = 0;j < i; ++ j) {                if (arr[j].len > arr[i].len && arr[j].wid > arr[i].wid) {                    dp[i] = max (dp[i],dp[j] + arr[i].high);                    ans = max (dp[i],ans);                }            }        }        cout << "Case "<< cas++ <<": maximum height = ";        cout << ans << endl;    }    return 0;}
原创粉丝点击