POJ3624 Charm Bracelet 【01背包】
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 22621 | Accepted: 10157 |
Description
Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible from the N (1 ≤ N ≤ 3,402) available charms. Each charm i in the supplied list has a weightWi (1
≤ Wi ≤ 400), a 'desirability' factor Di (1 ≤ Di ≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more than M (1 ≤ M ≤ 12,880).
Given that weight limit as a constraint and a list of the charms with their weights and desirability rating, deduce the maximum possible sum of ratings.
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: Line i+1 describes charm i with two space-separated integers: Wi and Di
Output
* Line 1: A single integer that is the greatest sum of charm desirabilities that can be achieved given the weight constraints
Sample Input
4 6
1 4
2 6
3 12
2 7
Sample Output
23
题意:给定物品数量n和背包容量m,n个物品的重量weight和价值val,求能获得的最大价值。
题解:状态转移方程:dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i]] + v[i]);当中状态dp[i][j]表示前i个物品放在容量为j的背包中能获得的最大价值。
二维数组能够压缩成一维以节省空间,可是内层循环须要倒序。
原始版本号:耗时360ms
#include <stdio.h>
#define maxn 12882 int dp[maxn]; int max(int a, int b){ return a > b ? a : b; } int main()
{
int n, totalWeight, i, j, weight, val;
scanf("%d%d", &n, &totalWeight);
for(i = 1; i <= n; ++i){
scanf("%d%d", &weight, &val);
for(j = totalWeight; j; --j){
if(j >= weight) dp[j] = max(dp[j], dp[j - weight] + val);
}
}
printf("%d\n", dp[totalWeight]);
return 0;
}<span style="font-family:FangSong_GB2312;">
</span>
优化后的代码:耗时219ms
#include <stdio.h>
#define maxn 12882 int dp[maxn]; int main()
{
int n, totalWeight, i, j, weight, val;
scanf("%d%d", &n, &totalWeight);
for(i = 1; i <= n; ++i){
scanf("%d%d", &weight, &val);
for(j = totalWeight; j; --j){
if(j >= weight && dp[j - weight] + val > dp[j])
dp[j] = dp[j - weight] + val;
}
}
printf("%d\n", dp[totalWeight]);
return 0;
}
用二维dp数组写了一个,果断的超了内存,占用内存大概12882*3404*4/1024=171兆。题目限制是65兆
TLE:
#include <stdio.h>
#define maxn 12882 int dp[3404][maxn]; int max(int a, int b){ return a > b ? a : b; } int main()
{
int n, m, weight, val, i, j;
scanf("%d%d", &n, &m);
for(i = 1; i <= n; ++i){
scanf("%d%d", &weight, &val);
for(j = 1; j <= m; ++j)
if(j >= weight)
dp[i][j] = max(dp[i-1][j], dp[i-1][j-weight] + val);
else dp[i][j] = dp[i-1][j];
}
printf("%d\n", dp[n][m]);
return 0;
}
POJ3624 Charm Bracelet 【01背包】的更多相关文章
- POJ 3624 Charm Bracelet(01背包)
Charm Bracelet Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34532 Accepted: 15301 ...
- POJ 3624 Charm Bracelet(01背包裸题)
Charm Bracelet Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38909 Accepted: 16862 ...
- 洛谷——2871[USACO07DEC]手链Charm Bracelet——01背包
题目描述 Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like t ...
- POJ 3624 Charm Bracelet(01背包模板题)
题目链接 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 52318 Accepted: 21912 Descriptio ...
- POJ 3624 Charm Bracelet 0-1背包
传送门:http://poj.org/problem?id=3624 题目大意:XXX去珠宝店,她需要N件首饰,能带的首饰总重量不超过M,要求不超过M的情况下,使首饰的魔力值(D)最大. 0-1背包入 ...
- 洛谷 P2871 [USACO07DEC]手链Charm Bracelet && 01背包模板
题目传送门 解题思路: 一维解01背包,突然发现博客里没有01背包的板子,补上 AC代码: #include<cstdio> #include<iostream> using ...
- poj3642 Charm Bracelet(0-1背包)
题目意思: 给出N,M,N表示有N个物品,M表示背包的容量.接着给出每一个物品的体积和价值,求背包可以装在的最大价值. http://poj.org/problem? id=3624 题目分析: o- ...
- Poj3624 Charm Bracelet (01背包)
题目链接:http://poj.org/problem?id=3624 Description Bessie has gone to the mall's jewelry store and spie ...
- [转]POJ3624 Charm Bracelet(典型01背包问题)
来源:https://www.cnblogs.com/jinglecjy/p/5674796.html 题目链接:http://bailian.openjudge.cn/practice/4131/ ...
随机推荐
- [SharePoint][SharePoint Designer 入门经典]Chapter11 工作流基础
1.SPS中可以创建的工作流的种类 2.SPD工作流基础 3.创建列表\库工作流 4.创建可重用的工作流 5.利用基于站点的工作流 6.SPD 工作流的限制和注意事项
- Python Study(02)之 Context Manager
上下文管理器(context manager)是Python2.5开始支持的一种语法,用于规定某个对象的使用范围.一旦对象进入或者离开该使用范围,会有特殊操作被调用 (比如为对象分配或者释放内存).它 ...
- mysql开发之---每日一得01
2015年7月7日------------------------- 1.truncate表会清空建表语句auto_increment的值:某个表的id即是主键也是自增,你能够选择插入随意id值,假设 ...
- 【分享】GEARS of DRAGOON 1+2【日文硬盘版】[带全CG存档&攻略+SSG改动+打开存档补丁]
冒险者们哟.寻找龙秘玉吧--! ninetail的最新作,是使用丰富多彩的技能·道具探索迷宫的3D迷宫RPG! 存在着骑士和神官的架空世界常见的职业为首的13种职业.超过数百种的道具的登场! 和伙伴一 ...
- java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "getClassLoader")
转自:https://blog.csdn.net/bluecard2008/article/details/80921682?utm_source=blogxgwz0 摘要: 今天在使用jetty做容 ...
- Vmware 安装samba之二
安装samba:sudo apt-get install samba 安装smbclient:sudo apt-get install 安装smbfs:sudo apt-get smbfs 2.修改配 ...
- Python 接口类或抽象类 反射
# 抽象类或者接口类,制定规范,统一方法名 # # 抽象类或者接口类,制定规范,统一方法名 from abc import ABCMeta,abstractmethod class Payrole(m ...
- 前端开发人员要注意的css规范,css命名。
刚工作的时候也没注意关于css的规则,根据自己的心情想怎么用就怎么用,完成工作就好不会考虑代码的可读性,加载的性能,现在身为前端的一员就要有程序员的自我修养(嘿嘿,是不是很有责任感啊). 废话不多说, ...
- 含神经网络的离线AI翻译 APP
功能特性 下载 https://www.microsoft.com/en-us/store/p/translator/9wzdncrfj3pg
- USACO Sabotage, 2014 Mar 破坏阴谋(二分+贪心)
一开始看完这题就有个想法: 只要把大于整个序列平均数的最大连续序列就是最优? 那把整个序列都减掉平均数 在做最大连续字序列和且记录长度? 仔细思考一下并不太对: 当子序列最大但长度较大 也许也比不上删 ...