hdu1069 Monkey and Banana 一个简单的动态规划

题目链接


前一天睡前看了一道题,睡觉的时候想了想,一大早(误)起来写了一下交了然后就AC了。

嘿!太难得了!(……

感觉是思路很简单的一个动态规划,然后也没有做任何优化,连双关键字排序也只是写的选择排序的......因为感觉数据量实在很小啊(其实是因为不熟悉怎么用sort...)。

首先读入的时候处理一下,把任意可能的砖块都存到数组里面,默认前两个为长宽,第三个为高。然后从大到小双关键字排序

状态转移方程就是 sum[i] = max(sum[j] + c[i]); (其中 j < i 且 a[j] > a[i] 且 b[j] > b[i] )


AC代码如下:

//1069.cpp

#include <iostream>
#define maxn 200
int a[maxn],b[maxn],c[maxn],sum[maxn];
using namespace std;
void sort(int a[],int b[],int c[],int n);
int main()
{
    int n,i,j,tot,maxx,ans,test=0;
    while (cin >> n && n!=0)
    {
        ++ test;
        tot = n;
        for (i = 0; i < n; ++ i)
        {
            cin >> a[i] >> b[i] >> c[i];
            if (a[i]==b[i] && b[i]==c[i])
                continue;
            if (a[i] == b[i])
            {
                a[tot] = c[tot] = a[i]; b[tot] = c[i];
                b[tot] = c[tot] = a[i]; a[tot++] = c[i];
                continue;
            }
            if (b[i] == c[i])
            {
                a[tot] = b[tot] = b[i]; c[tot] = a[i];
                a[tot] = c[tot] = b[i]; b[tot++] = a[i];
                continue;
            }
            if (a[i] == c[i])
            {
                a[tot] = b[tot] = a[i]; c[tot] = b[i];
                b[tot] = c[tot] = a[i]; a[tot++] = b[i];
                continue;
            }
            a[tot] = a[i]; b[tot] = c[i]; c[tot++] = b[i];
            a[tot] = b[i]; b[tot] = a[i]; c[tot++] = c[i];
            a[tot] = b[i]; b[tot] = c[i]; c[tot++] = a[i];
            a[tot] = c[i]; b[tot] = a[i]; c[tot++] = b[i];
            a[tot] = c[i]; b[tot] = b[i]; c[tot++] = a[i];
        }
        sort(a,b,c,tot);
        sum[0] = c[0];
        ans = 0;
        for (i = 1; i < tot; ++ i)
        {
            maxx = 0;
            for (j = 0; j < i; ++ j)
                if (a[j] > a[i] && b[j] > b[i])
                    maxx = max(maxx, sum[j]);
            sum[i] = maxx + c[i];
            ans = max(ans, sum[i]);
        }
        cout << "Case " << test << ": maximum height = " << ans << endl;
    }
    return 0;
}
void sort(int a[],int b[],int c[],int n)
{
    int i, j, temp;
    for (i = 0; i < n; ++ i)
        for (j = i+1; j < n; ++ j)
        {
            if (a[i] < a[j])
            {
                temp = a[i]; a[i] = a[j]; a[j] = temp;
                temp = b[i]; b[i] = b[j]; b[j] = temp;
                temp = c[i]; c[i] = c[j]; c[j] = temp;
            }
            if (a[i] == a[j] && b[i] < b[j])
            {
                temp = b[i]; b[i] = b[j]; b[j] = temp;
                temp = c[i]; c[i] = c[j]; c[j] = temp;
            }
        }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值