POJ 2392 简单dp 多重背包
2015-12-23 13:30
387 查看
题意
有很多种石头,每种石头的高度是hi,但是不能放到ai之上的高度,并且这种石头有ci个将这些石头叠加起来,问能够达到的最高高度
思路
看白书上说是稍加思考的题目。。。为什么我觉得还没有优化递推关系的那些题难。。。和一般背包稍有不同的就是对石头按ai排序。。。然后再做。。这个很好理解这里就不多说了。。
这样纯做,复杂度有点虚,不过dp常数小。。试了试就过了。。
实现
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; const int maxn = 405; const int maxm = 40005; struct Node{ int a,c,h; }node[maxn]; bool operator<(Node x,Node y){ return x.a < y.a; } int dp[maxm]; int main(){ int n; scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d%d%d",&node[i].h,&node[i].a,&node[i].c); sort(node+1,node+n+1); node[0].h = node[0].a = node[0].c = 0; for (int i=1;i<=n;i++){ for (int j=node[i].a;j>0;j--){ if (j > node[i-1].a) dp[j] = dp[node[i-1].a]; for (int k=1;k<=node[i].c;k++){ int tmp = j - node[i].h * k; if (tmp < 0){ break; } if (tmp > node[i-1].a) { dp[j] = max(dp[node[i-1].a]+node[i].h * k,dp[j]); } else{ dp[j] = max(dp[tmp]+node[i].h * k,dp[j]); } } } } cout << dp[node .a] << "\n"; return 0; }
相关文章推荐
- discuz mysql 错误 (1045) notconnect
- 他山之石-php缓存技术总结
- online_judge_1142
- 重载运算符
- 第三天 绘制数字
- 重写(覆盖)、重载的区别
- 20个Linux防火墙[iptables]应用技巧[转]
- Mysql 查看定时器 打开定时器 设置定时器时间
- 查询iptables编译了哪些模块
- writing idiomatic python 读书笔记(1)
- 单片机温度控制系统DS18B20
- 单片机温度控制系统DS18B20
- Java中的线程池——Executor框架
- QQ空间接口
- 151223怎么去掉Xcode工程中的某种类型的警告 Implicit conversion loses integer precision: 'NSInteger' (aka 'long') to
- 制作血条
- ScrollView 的使用(非原创)
- Struts2文件上传
- MaterialDesign 之 ToolBar学习
- SQL Tuning 基础概述09 - SQL Access Advisor