Charm Bracelet
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背包】的更多相关文章

  1. POJ 3624 Charm Bracelet(01背包)

    Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34532   Accepted: 15301 ...

  2. POJ 3624 Charm Bracelet(01背包裸题)

    Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38909   Accepted: 16862 ...

  3. 洛谷——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 ...

  4. POJ 3624 Charm Bracelet(01背包模板题)

    题目链接 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 52318   Accepted: 21912 Descriptio ...

  5. POJ 3624 Charm Bracelet 0-1背包

    传送门:http://poj.org/problem?id=3624 题目大意:XXX去珠宝店,她需要N件首饰,能带的首饰总重量不超过M,要求不超过M的情况下,使首饰的魔力值(D)最大. 0-1背包入 ...

  6. 洛谷 P2871 [USACO07DEC]手链Charm Bracelet && 01背包模板

    题目传送门 解题思路: 一维解01背包,突然发现博客里没有01背包的板子,补上 AC代码: #include<cstdio> #include<iostream> using ...

  7. poj3642 Charm Bracelet(0-1背包)

    题目意思: 给出N,M,N表示有N个物品,M表示背包的容量.接着给出每一个物品的体积和价值,求背包可以装在的最大价值. http://poj.org/problem? id=3624 题目分析: o- ...

  8. Poj3624 Charm Bracelet (01背包)

    题目链接:http://poj.org/problem?id=3624 Description Bessie has gone to the mall's jewelry store and spie ...

  9. [转]POJ3624 Charm Bracelet(典型01背包问题)

    来源:https://www.cnblogs.com/jinglecjy/p/5674796.html 题目链接:http://bailian.openjudge.cn/practice/4131/ ...

随机推荐

  1. 【机房重构】—上机&amp;订餐

    前几天通过UML图中的时序图.让我对于机房重构中的每一条线理解的更加清晰.曾经认为上机特别的乱,在一次偶遇中,得知了原来它能够转化成我们平时订餐.以下就听我说一说上机&订餐的故事吧! 又是发生 ...

  2. [总结]FFMPEG视音频编解码零基础学习方法【转】

    本文转载自:http://blog.csdn.net/leixiaohua1020/article/details/15811977 在CSDN上的这一段日子,接触到了很多同行业的人,尤其是使用FFM ...

  3. jqueryui slider

    <!doctype html><html lang="en"><head> <meta charset="utf-8" ...

  4. sql:String格式转换为时间进行比较

    字符串的格式为 yyyy-MM-dd HH:mm:ss str_to_date(a.time, '%Y-%m-%d %k:%i') < str_to_date(b.time, '%Y-%m-%d ...

  5. app专项测试

    本节为大家讲述app的专项测试——客户端性能测试.这个我也做了蛮久的了.在这里修改了一下本篇随笔. 首先我们了解一下什么是客户端的性能测试.性能测试相比大家都已经耳熟能详了,这个app的客户端性能测试 ...

  6. 我的nginx+php是如何配置的?

    nginx使用homebrew安装,安装之后 ngxin 安装目录:/usr/local/Cellar/nginx/1.8.0 删除掉默认的www目录,创建一个自己方便找到的 删除掉默认的www目录 ...

  7. JS判断客户端是否是iOS或者Android或者ipad(一)

    通过判断浏览器的userAgent,用正则来判断是否是ios和Android客户端.代码如下<script type="text/javascript"> var u ...

  8. Pyhton学习——Day7

    ##############################################匿名函数################################################## ...

  9. echart纵坐标标签特别长换行显示

    纵坐标 yAxis : [ { type : 'category', data : name, axisLabel: { //坐标轴刻度标签的相关设置. textStyle: { color: '#0 ...

  10. [TJOI2015]线性规划

    题目:洛谷P3973.BZOJ3996. 题目大意:给你n,参数b[][]和c[](里面的数均>0),要你求一个数组A[](0/1,1表示选择),已知:1. 若同时选择X和Y,获得B[x][y] ...