zoukankan      html  css  js  c++  java
  • HDU_1069 Monkey and Banana (DP)

      这个题目与最长上升子序类问题类似,刚开始想用贪心做了,但是悲剧的连题目给的数据都没过。后来想DP,由于最近在看背包问题,思维僵化,怎么想也想不出怎么套背包的转移方程。问了下高手,才猛然想起来这题的思路,我悲剧的一晚上啊。。。。

    代码:

    #include <iostream>
    #include
    <cstdio>
    #include
    <cstdlib>

    using namespace std;
    const int N = 31;

    struct blocks
    {
    int l;
    int w;
    int h;
    int s;
    }b[N
    *6];
    int dp[N*6];
    int cmp(const void * a, const void * b)
    {
    return (*(blocks *)a).s - (*(blocks *)b).s;
    }
    int main()
    {
    //freopen("data.in", "r", stdin);

    int x, y, z, n, i, j, cas = 0;
    while(scanf("%d", &n), n)
    {
    int k = 0;
    while(n--)
    {
    scanf(
    "%d%d%d", &x, &y, &z);
    b[k].l
    = x; b[k].w = y; b[k].h = z; b[k].s = b[k].l * b[k].w; k++; //六种组合情况
    b[k].l = x; b[k].w = z; b[k].h = y; b[k].s = b[k].l * b[k].w; k++;
    b[k].l
    = y; b[k].w = x; b[k].h = z; b[k].s = b[k].l * b[k].w; k++;
    b[k].l
    = y; b[k].w = z; b[k].h = x; b[k].s = b[k].l * b[k].w; k++;
    b[k].l
    = z; b[k].w = x; b[k].h = y; b[k].s = b[k].l * b[k].w; k++;
    b[k].l
    = z; b[k].w = y; b[k].h = x; b[k].s = b[k].l * b[k].w; k++;
    }
    qsort(b, k,
    sizeof(blocks), cmp);
    int ans;
    for(i = 0; i < k; i++)
    dp[i]
    = b[i].h;
    for(i = 1; i < k; i++)
    {
    ans
    = 0;
    for(j = 0; j < i; j++)
    if(b[i].l > b[j].l && b[i].w > b[j].w && ans < dp[j])
    ans
    = dp[j];
    dp[i]
    += ans;
    }
    ans
    = 0;
    for(i = 0; i < k; i++)
    if(ans < dp[i])
    ans
    = dp[i];
    printf(
    "Case %d: maximum height = %d\n", ++cas, ans);
    }
    return 0;
    }
  • 相关阅读:
    flock对文件锁定读写操作的问题 简单
    hdu 2899 Strange Fuction(二分)
    hdu 2199 Can you solve this equation? (二分)
    poj 3080 Blue Jeans (KMP)
    poj 2823 Sliding Window (单调队列)
    poj 2001 Shortest Prefixes (trie)
    poj 2503 Babelfish (trie)
    poj 1936 All in All
    hdu 3507 Print Article (DP, Monotone Queue)
    fzu 1894 志愿者选拔 (单调队列)
  • 原文地址:https://www.cnblogs.com/vongang/p/2156428.html
Copyright © 2011-2022 走看看