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 */
- hdu_1078_FatMouse and Cheese_神奇的贪心_动态规划_算不上是dp
- 动态规划_树形DP
- 动态规划_数位DP
- 动态规划_区间DP
- 动态规划_线性结构上的DP
- 动态规划_线性结构上的DP
- 动态规划_复杂状态DP
- 神奇的贪心 + DP
- 数据结构_动态规划
- 动态规划_总结
- 动态规划_初始
- 报数_动态规划
- 6_动态规划_金明的预算方案
- 动态规划_数字的划分
- 动态规划_动态规划算法的优化技巧
- POJ 1157 LITTLE SHOP OF FLOWERS (插画的最大值_经典的动态规划DP)
- 算法_动态规划_货币兑换
- 算法_动态规划_图像压缩
- ecfr 26 A. Text Volume
- 链表排序问题(leetcode试题)
- PVAnet的配置和安装
- 删除文件时候显示“文件已在资源管理器中打开”快速解决方法
- Linux之进程
- hdu_1078_FatMouse and Cheese_神奇的贪心_动态规划_算不上是dp
- Linux学习笔记_系统登陆及欢迎信息(/etc/motd文件,vi)
- vi光标移动命令之二
- 中间件之权限控制
- 网易编程题 彩色的砖块
- MySQL导入Hive
- 爬取百度贴吧图片
- [第七季]10.获取或设置样式和尺寸
- 剑指offer之矩形覆盖