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/ ...
随机推荐
- 洛谷 U249 匹配
U249 匹配 题目描述 输入整数s和两个整数集合A和B,从这A和B中各取一个数,如果它们的和等于s,称为“匹配”.编程统计匹配的总次数 输入输出格式 输入格式: 第一行为三个整数s(0<s≤1 ...
- Android Touch事件分发过程
虽然网络上已经有非常多关于这个话题的优秀文章了,但还是写了这篇文章,主要还是为了加强自己的记忆吧,自己过一遍总比看别人的分析要深刻得多.那就走起吧. 简单演示样例 先看一个演示样例 : 布局文件 : ...
- 菜鸟的mongoDB学习---(六)MongoDB 索引
MongoDB 索引 ps:大概有半个月木有更新了,因为前一阶段的出差和这几天突然来的项目.导致上网时间急剧降低,实在是sorry,以后预计会好一点. 索引通常可以极大的提高查询的效率.假设没有索引. ...
- 沃通SSL精灵,让站点HTTPS永只是期
告别HTTP明文"裸奔"时代 百度.阿里巴巴.必应等越来越多的互联网巨头相继启用全站HTTPS加密,保护用户数据和隐私安全.逐步告别HTTP明文"裸奔"时代. ...
- Xcode6 引入第三方静态库project的方法
首先.介绍一下把在当前project中引入其它依赖project的方法: 第一:把其它项目project加入到现有project做法: 定义: FPro 现有project == 父project C ...
- iOS 8使用Touch ID进行身份认证
iOS 8的SDK开放了Touch ID的接口.从WWDC的视频中能够看到Touch ID应用在两个方面:用于Key Chain加密和用于授权.iOS 8正式版公布以后我们能够看到Evernote的i ...
- BZOJ 1790: [Ahoi2008]Rectangle 矩形藏宝地
BZOJ 1790: [Ahoi2008]Rectangle 矩形藏宝地 题目传送门 [题目大意] 游戏的主办方把这块开阔地当作第一象限,将所有可能埋藏宝藏的地方划成一个个矩形的土地,并把这些矩形土地 ...
- 内存问题检测神器:Valgrind
Linux下内存问题检测神器:Valgrind 在写大型C/C++工程时难免会发生内存泄漏现象,系统编程中一个重要的方面就是有效地处理与内存相关的问题.你的工作越接近系统,你就需要面对越多的内存问题. ...
- web前端简单布局
jquery实现的计算器
- 原生js实现复选框
简单排版 <style> .box { display: flex; align-items: center; } #allSelect, p { width: 20px; height: ...