Charm Bracelet
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 47440   Accepted: 20178

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 weight Wi (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

如果用dp[n][m]:前n个物品不超过体积m的最大价值,状态转移方程为:

dp[i][j] = 0(i==0orj==0)//没有物品,背包体积再大的最大价值也是0;再多物品,背包体积为0都塞不下。

dp[i][j]=dp[i-1][j](j<d[i])//当前背包塞不下第i个物品,最大价值和第i-1个物品不大于体积j的最大价值是一样的。

dp[i][j]=max(dp[i-1][j],dp[i-1][j-d[i]]+w[i])//当前背包若能装下第i个物品,那么就在装第i个物品后的最大价值和不装第i个物品的最大价值中取最大值

但由于N和M的范围太大,开二维数组会爆内存,分析求解过程发现求解dp[i][j]时只与它上一行的正上方和上一行左边的某个值有关,也就是只用到了它上面那行,可以用一个滚动的一维数组求解,把新值放到上面的位置,但要注意顺序必须是从右到左,不然会覆盖掉有用的值。

 #include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int n, m;
int d[], w[];
int dp[];//因为求解该行时只与上一行有关,所以可以用滚动数组,dp[j]表示前i个物品在不超过体积j的最大价值
cin >> n >> m;
for (int i = ; i <= n; ++i)
{
cin >> d[i] >> w[i];
}
memset(dp, , sizeof(dp));
for (int i = ; i <= n; ++i)
{
for (int j = m; j >= ; --j)//从右往左求解,把新求出的值保存在上面的位置,因为被覆盖的值只与它下面和下一行的右边有关
{
if (j >= d[i])
dp[j] = max(dp[j], dp[j - d[i]] + w[i]);
else //如果j-d[i]<0,说明当前背包的容量不够放第i个物品,那放前i个物品的最大价值与放前i-1个物品的最大价值是相等的,
break;//又因为从右往左求值,左边的j只会更小,因此可跳过节省时间 }
}
cout << dp[m] << endl;
return ;
}

POJ3624 0-1背包(dp+滚动数组)的更多相关文章

  1. HDU 5119 Happy Matt Friends (背包DP + 滚动数组)

    题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt ...

  2. 算法笔记(c++)--关于01背包的滚动数组

    算法笔记(c++)--关于01背包的滚动数组 关于01背包问题:基本方法我这篇写过了. https://www.cnblogs.com/DJC-BLOG/p/9416799.html 但是这里数组是N ...

  3. HDU 1024 Max Sum Plus Plus --- dp+滚动数组

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

  4. POJ 3666 Making the Grade (DP滚动数组)

    题意:农夫约翰想修一条尽量平缓的路,路的每一段海拔是A[i],修理后是B[i],花费|A[i] – B[i]|,求最小花费.(数据有问题,代码只是单调递增的情况) #include <stdio ...

  5. USACO 2009 Open Grazing2 /// DP+滚动数组oj26223

    题目大意: 输入n,s:n头牛 s个栅栏 输入n头牛的初始位置 改变他们的位置,满足 1.第一头与最后一头的距离尽量大 2.相邻两头牛之间的距离尽量满足 d=(s-1)/(n-1),偏差不超过1 3. ...

  6. BZOJ-1925 地精部落 烧脑DP+滚动数组

    1925: [Sdoi2010]地精部落 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1053 Solved: 633 [Submit][Status ...

  7. hdu 1513 && 1159 poj Palindrome (dp, 滚动数组, LCS)

    题目 以前做过的一道题, 今天又加了一种方法 整理了一下..... 题意:给出一个字符串,问要将这个字符串变成回文串要添加最少几个字符. 方法一: 将该字符串与其反转求一次LCS,然后所求就是n减去 ...

  8. tyvj P1519 博彩游戏(AC自动机+DP滚动数组)

    P1519 博彩游戏 背景 Bob最近迷上了一个博彩游戏…… 描述 这个游戏的规则是这样的:每花一块钱可以得到一个随机数R,花上N块钱就可以得到一个随机序列:有M个序列,如果某个序列是产生的随机序列的 ...

  9. poj1159 dp(滚动数组优化)

    H - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:3000MS     Memory Limit:65536KB     ...

随机推荐

  1. centOS命令随笔记(杂):cd、pwd

    1.向上向下翻页: 反向的命令一般是Shift-PageUp和Shift-PageDown. 每日一命令:cd 1.cd /   进入系统根目录 2.cd ..   进入系统根目录可以使用“ cd . ...

  2. E20170611-hm

    ascending   adj. 上升的,向上的; ascend   vt. 攀登; 继承; 占领;   vi. 上升; 爬坡; 追溯; descending  n. 递减;  descend   v ...

  3. unable to unroll loop 报错

    unable to unroll loop, loop does not appear to terminate in a timely manner (1024 iterations) 原本代码 f ...

  4. 什么是GFW

    GFW(Great Firewall of China)中文名:中国国家防火墙,建立于1998年.我们平常所说的“被墙了”,是指网站内容或服务被防火墙屏蔽了.而“FQ”是指突破防火墙去浏览那些被屏蔽的 ...

  5. 二分搜索 POJ 3258 River Hopscotch

    题目传送门 /* 二分:搜索距离,判断时距离小于d的石头拿掉 */ #include <cstdio> #include <algorithm> #include <cs ...

  6. svn报错:Cannot negotiate authentication mechanism

    在使用eclipse的svn插件连接osc的代码仓库时候,发生了以下错误: Cannot negotiate authentication mechanismsvn: Unable to connec ...

  7. Android 性能优化(27)*zipalign让apk数据对齐,运行更快。

    1.zipalign 简介 zipalign is an archive alignment tool that provides important optimization to Android ...

  8. 236 Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先

    给定一棵二叉树, 找到该树中两个指定节点的最近公共祖先. 详见:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tre ...

  9. Objective-C设计模式——中介者Mediator(对象去耦)

    中介者模式 中介者模式很好的诠释了迪米特法则,任意两个不相关的对象之间如果需要关联,那么需要通过第三个类来进行.中介者就是把一组对象进行封装,屏蔽了类之间的交互细节,使不同的类直接不需要持有对方引用也 ...

  10. Font Awesome 图标使用总结

    参考 http://fontawesome.dashgame.com/ 1 大图标递进  fa-lg (33%递增).fa-2x. fa-3x.fa-4x,或者 fa-5x  2 固定宽度  fa-f ...