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

来源:互联网 发布:反屏蔽器软件 编辑:程序博客网 时间:2024/05/22 13:52

题目链接


前一天睡前看了一道题,睡觉的时候想了想,一大早(误)起来写了一下交了然后就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 200int 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;            }        }}


0 0
原创粉丝点击