hdu 3466 Proud Merchants(01背包)(微变化,要理解)
2015-12-24 20:04
399 查看
hdu 3466 Proud Merchants
分析
这道题题目的意思,就是在基础背包重量和价格之外,还告诉你了选这件物品时,钱数要高于一定数目才可以选择。正确的做法是先将物品按照(要求达到的钱数-价格)从小到大排个序,然后01背包即可。当初自己做的时后,也感觉要排个序,但至今,还是觉得很玄。我的理解是,为保证动归方程无后效性,先将影响范围大的先计算,或者是更“包容”的先计算,这样前面更新过的状态不会影响后面更新的状态。留个心眼。
题目
http://acm.hdu.edu.cn/showproblem.php?pid=3466代码
#include <iostream> #include <cstring> #include <ctime> #include <fstream> #include <cstdlib> #include <algorithm> #include <set> #include <vector> using namespace std; int dp[50010]; struct node { int p,q,v; bool operator <(node a) const { return (q-p)<(a.q-a.p); } }; node thi[510]; int main() { int N,M; while(cin>>N>>M) { memset(dp,0,sizeof(dp)); for(int i=1;i<=N;i++) { cin>>thi[i].p>>thi[i].q>>thi[i].v; } sort(thi+1,thi+N+1); for(int i=0;i<=N;i++) for(int j=M;j>=thi[i].q;j--) dp[j]=max(dp[j],dp[j-thi[i].p]+thi[i].v); cout<<dp[M]<<endl; } return 0; }
相关文章推荐
- QT中的show()与exec()区别
- 愚人节的礼物(杭电1870)(水题一道~)
- nexus中自定义仓库
- [LeetCode116]Path Sum
- BZOJ 1012 最大数
- TCP 滑动窗口 (流量控制)
- 程序设计箴言
- 【C++标准】之 C++11——auto特性
- 解决 ubuntu14.04下的gedit中文乱码
- 使用System Sound Services 播放音效(最简单,比较底层),调用AudioServicesPlaySystemSound()
- 对 C++ 历史的个人观点
- Oracle创建表空间、创建用户以及授权、查看权限
- str.replace(/[ ]/g, "");
- 【Unity游戏开发之十四】iOS9 ReplayKit录制视频
- LeetCode 039 Combination Sum
- SubMenu子菜单
- Redis整合Spring结合使用缓存实例
- Echache整合Spring缓存实例讲解
- CenterOS 6.5操作系统 Nginx下配置 NodeJS
- 如何切换eclipse中svn用户