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/ ...
随机推荐
- 浙大PAT考试1013~1016(最伤的一次。。)
我能说我1016WA了几天都不得最后还是拿别人代码交的么. .. 真心找不到那个神数据.. . 自己把整个程序的流程都画出来了.细致推敲是木有问题的啊... 题目地址:点击打开链接 先从1013開始介 ...
- HDU 4893 线段树裸题
Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- cocos2D(六)----CCLayer
一个游戏中能够有非常多个场景,每一个场景里面又可能包括有多个图层,这里的图层一般就是CCLayer对象.CCLayer本身差点儿没什么功能.对照CCNode,CCLayer可用于接收触摸和加速计输入. ...
- No unique bean of type [net.shougongfang.action.paymoney.AlipayPayMoneyReturnObj] is defined: Unsat
0 你把@Service放到实现类上吧.这个问题好像不止一个人在问啦 2013年10月25日 10:34 shidan66 30 0 1 1 加入评论 00 1,@service放到实现上 2. ...
- android:QQ多种側滑菜单的实现
在这篇文章中写了 自己定义HorizontalScrollView实现qq側滑菜单 然而这个菜单效果仅仅是普通的側拉效果 我们还能够实现抽屉式側滑菜单 就像这样 第一种效果 另外一种效果 第三种效果 ...
- windows下PTAM的编译
前些日子在研究PTAM,以下首先说说PTAM的编译过程,我在XP几WIN7搭配vs2010中均已測试过,都能够执行. 首先下载编译PTAM所必须的库文件.下载地址我会给出 PTAM(PTAM.zip) ...
- HDU 5493 Queue 树状数组+二分
Queue Problem Description N people numbered from 1 to N are waiting in a bank for service. They all ...
- 英语发音规则---P字母
英语发音规则---P字母 一.总结 一句话总结: 1.P发[p]音? paper ['peɪpə] n. 纸 plane [pleɪn] n. 飞机 pig [pɪg] n. 猪 ship [ʃɪp] ...
- 初探MVC路由
文章目录: 1.认识理解URL,以及简单的路由 2.特性路由.传统路由.区域路由 3.路由生成URL&&绑定到操作&&路由约束 1.认识理解URL,以及简单的路由 默 ...
- nginx高级-前端必会
需要设置的几个参数: 基本配置文件 user www www; worker_processes auto; error_log /www/wwwlogs/nginx_error.log crit; ...