HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)
HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)
题意分析
要先排序,在做01背包,否则不满足无后效性,为什么呢?
等我理解了再补上。
代码总览
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define nmax 505
#define nn 505*100
using namespace std;
struct item{
int p;
int q;
int v;
int rate;
}a[nmax];
int dp[nn];
bool cmp(item x, item y)
{
return x.rate<y.rate;
}
int main()
{
//freopen("in.txt","r",stdin);
int N,M;
while(scanf("%d%d",&N,&M)!= EOF){
memset(dp,0,sizeof(dp));
for(int i = 1; i<= N;++i) {scanf("%d%d%d",&a[i].p,&a[i].q,&a[i].v); a[i].rate = a[i].q - a[i].p;}
sort(a+1,a+1+N,cmp);
for(int i = 1; i<=N;++i){
for(int j =M;j>=a[i].q;--j)
dp[j] = max(dp[j],dp[j-a[i].p]+a[i].v);
}
printf("%d\n",dp[M]);
}
return 0;
}
HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)的更多相关文章
- HDOJ(HDU).2546 饭卡(DP 01背包)
HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...
- HDOJ(HDU).2602 Bone Collector (DP 01背包)
HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...
- UVA 562 Dividing coins(dp + 01背包)
Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were figh ...
- uva 562 Dividing coins(01背包)
Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were f ...
- hdu 1561【树形dp+01背包】
http://acm.hdu.edu.cn/showproblem.php?pid=1561 很容易想到如果是要攻克v城需要先攻克u城的话,可以建u到v的边.但是如果能够直接攻克u城呢?无边可建,这样 ...
- UVA 562 Dividing coins (01背包)
题意:给你n个硬币,和n个硬币的面值.要求尽可能地平均分配成A,B两份,使得A,B之间的差最小,输出其绝对值.思路:将n个硬币的总价值累加得到sum, A,B其中必有一人获得的钱小于等于sum/2 ...
- HDU 3466 Proud Merchants(0-1背包)
http://acm.hdu.edu.cn/showproblem.php?pid=3466 题意: 最近,iSea去了一个古老的国家.在这么长的时间里,它是世界上最富有和最强大的王国.结果,这个国家 ...
- UVA 562 Dividing coins【01背包 / 有一堆各种面值的硬币,将所有硬币分成两堆,使得两堆的总值之差尽可能小】
It's commonly known that the Dutch have invented copper-wire. Two Dutch men were fighting over a nic ...
- HDU 3466 Proud Merchants(01背包)
这道题目看出背包非常easy.主要是处理背包的时候须要依照q-p排序然后进行背包. 这样保证了尽量多的利用空间. Proud Merchants Time Limit: 2000/1000 MS (J ...
随机推荐
- json模块使用总结——Python
# 原创文章,未经允许请勿转载 通过Python的json模块,可以将字符串形式的json数据转化为字典,也可以将Python中的字典数据转化为字符串形式的json数据. 之前使用这个模块时,都是随用 ...
- Mac环境下RabbitMq安装与测试教程
RabbitMq安装与测试教程 Installing on Mac I. 安装 123456789 brew install rabbitmq ## 进入安装目录cd /usr/local/Cella ...
- python基础之变量和简单数据类型
1.1 变量的命名和使用规范 变量名可以包含数字.字母.下划线,但是不能以数字开头. 变量名不能包含空格,可使用下划线来分割其中的单词. 不要将Python关键字和函数名用作变量名. 变量名应既简短又 ...
- 【scroll-view】 可滚动视图组件说明
scroll-view 可滚动视图容器 原型: <scroll-view scroll-x="[Boolean]" scroll-y="[Boolean]" ...
- python3-声音处理
先来说下二进制读写文件,这需要struct库 #二进制文件读写 import struct a= b=- # print(struct.pack("h",b)) # print(s ...
- 【转载】appium 操作汇总
'''.appium api第二弹 锋利的python,这是初稿,2015/1/5 如有错误的地方,请同学们进行留言,我会及时予以修改,尽量整合一份ok的api 作者:Mads Spiral QQ:7 ...
- 自动对象&局部静态对象
一.关键点 对象的生命周期:程序执行过程中,该对象存在的那段时间 局部对象:形参.函数体内部定义的变量 二.自动对象 自动对象:只存在于块执行期间的对象 包括:局部变量.形参 三.局部静态对象 特点: ...
- Calculator PartⅢ
GitHub/object-oriented The title of the work 这次敲代码耗时相对较短,但是始终无法完成debug步骤,目前上传的代码可以通过编译,但运行即报停,问题调试为内 ...
- 用SC命令 添加或删除windows服务提示OpenSCManager 失败5 拒绝访问
在安装命令行中安装 windowsOpenSCManager 失败5 的错误,原因是当前用户的权限不足,需要做的是在注册表 HKEY_LOCAL_MACHINE\Software\Microsof ...
- PAT 甲级 1048 Find Coins
https://pintia.cn/problem-sets/994805342720868352/problems/994805432256675840 Eva loves to collect c ...