hdu_1078_FatMouse and Cheese_神奇的贪心_动态规划_算不上是dp

来源:互联网 发布:wifi计费软件 编辑:程序博客网 时间:2024/05/13 23:31

FatMouse and Cheese
FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid location is labelled (p,q) where 0 <= p < n and 0 <= q < n. At each grid location Fatmouse has hid between 0 and 100 blocks of cheese in a hole. Now he’s going to enjoy his favorite food.

FatMouse begins by standing at location (0,0). He eats up the cheese where he stands and then runs either horizontally or vertically to another location. The problem is that there is a super Cat named Top Killer sitting near his hole, so each time he can run at most k locations to get into the hole before being caught by Top Killer. What is worse – after eating up the cheese at one location, FatMouse gets fatter. So in order to gain enough energy for his next run, he has to run to a location which have more blocks of cheese than those that were at the current hole.

Given n, k, and the number of blocks of cheese at each grid location, compute the maximum amount of cheese FatMouse can eat before being unable to move.
Input
There are several test cases. Each test case consists of

a line containing two integers between 1 and 100: n and k
n lines, each with n numbers: the first line contains the number of blocks of cheese at locations (0,0) (0,1) … (0,n-1); the next line contains the number of blocks of cheese at locations (1,0), (1,1), … (1,n-1), and so on.
The input ends with a pair of -1’s.
Output
For each test case output in a line the single integer giving the number of blocks of cheese collected.
Sample Input
3 1
1 2 5
10 11 6
12 12 7
-1 -1
Sample Output
37
Source
Zhejiang University Training Contest 2001
题意 给你n种立方体,告诉你长宽高,可以转动,每类都有无限个,把他们堆起来,规则是上面一个立方体的地面长宽都要小于下面一个的长宽,求堆起来的立方体的最大高度;
思路;
对长度或者宽度排序,之后对每一个点都进行贪心dp数组的内容是当前的最大高度,之后看每一个立方体的高度,是否能和之前的dp值相加,能相加就加上,dp[i]保留所有可以相加中的最大值.

#include<iostream>#include<cstdio>#include<cstring>#include<map>#include<algorithm>using namespace std;typedef long long ll;struct node{    int x,y,z;} num[11111];bool cmp(node a,node b){    if(a.x==b.x)        return a.y>b.y;    return a.x>b.x;}int main(){    int n,i,j,k;    int tt=0;    while(cin>>n&&n)    {        k=0;        for(i=0; i<n; i++)        {            int x,y,z;            scanf("%d%d%d",&x,&y,&z);            num[k].x=x;            num[k].y=y;            num[k].z=z;            k++;            num[k].x=y;            num[k].y=z;            num[k].z=x;            k++;            num[k].x=z;            num[k].y=x;            num[k].z=y;            k++;            num[k].x=y;            num[k].y=x;            num[k].z=z;            k++;            num[k].x=z;            num[k].y=y;            num[k].z=x;            k++;            num[k].x=x;            num[k].y=z;            num[k].z=y;            k++;        }        sort(num,num+k,cmp);        int ans=0;        int dp[111111]={0};        for(i=0; i<k; i++)        {            dp[i]=num[i].z;            for(j=0; j<i; j++)            {                if(num[j].x>num[i].x&&num[j].y>num[i].y)                {                   dp[i]=max(dp[i],dp[j]+num[i].z);                }            }        }        for(i=0;i<k;i++)        ans=max(ans,dp[i]);        tt++;        cout<<"Case "<<tt<<": maximum height = "<<ans<<endl;    }    return 0;}/*a s  d   f    a     s      s      */
原创粉丝点击