【nyoj - 860】 又见0-1背包 (dp,反向0-1背包,好题好思路)

题干:

又见01背包

时间限制:1000 ms  |  内存限制:65535 KB

难度:3

输入

多组测试数据。
每组测试数据第一行输入,n 和 W ,接下来有n行,每行输入两个数,代表第i个物品的wi 和 vi。

输出

满足题意的最大价值,每组测试数据占一行。

样例输入

4 5
2 3
1 2
3 4
2 2

样例输出

7

来源

飘谊系列

上传者

TC_张友谊

描述

    有n个重量和价值分别为wi 和 vi 的 物品,从这些物品中选择总重量不超过 W 

的物品,求所有挑选方案中物品价值总和的最大值。

  1 <= n <=100

  1 <= wi <= 10^7

  1 <= vi <= 100

  1 <= W <= 10^9

解题报告:

     因为这题w的数据量很大,显然开不下这么大的空间,但是反过来想,这题可以认为是在相同价值中更新所需背包容量最小的那一种,所以这题记录一下最大的价值v,然后以v为数组下标跑0-1背包就可以了。注意初始化问题哦,需要初始化成INF。

 还有一道题跟这题很像:【 FZU - 2214 】Knapsack problem

AC代码:

#include<bits/stdc++.h>using namespace std;
const int INF = 0x3f3f3f3f;
int w[105],v[105];
int dp[10000 + 5];
int n,m; 
int main()
{while(cin>>n>>m) {int sum = 0;for(int i = 1; i<=n; i++) {cin>>w[i]>>v[i];sum += v[i];}memset(dp,INF,sizeof(dp));dp[0] = 0;for(int i = 1; i<=n; i++) {for(int j = sum; j>=v[i]; j--) {dp[j] = min(dp[j],dp[j - v[i]] + w[i]) ;}}int ans = 0;for(int i = sum; i>=0; i--) {if(dp[i] <= m) {ans = i;break;} }cout << ans << endl;}return 0 ;} 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441757.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

300英雄服务器维护多久,《300英雄》2021年5月20日6:00-9:00更新维护公告

尊敬的《300英雄》玩家:《300英雄》将于2021年5月20日6:00-9:00(星期四)&#xff0c;对所有大区进行停机更新&#xff0c;更新期间&#xff0c;您将无法登录游戏。如果在预定时间内无法完成维护内容&#xff0c;开服时间也将继续顺延。具体更新内容如下&#xff1a;一、活动相关…

c 连接mysql示例 源码_MySQL 连接

MySQL 连接使用mysql二进制方式连接您可以使用MySQL二进制方式进入到mysql命令提示符下来连接MySQL数据库。实例以下是从命令行中连接mysql服务器的简单实例&#xff1a;[[email protected]]# mysql -u root -pEnter password:******在登录成功后会出现 mysql> 命令提示窗口…

【 FZU - 2214 】Knapsack problem(逆向0-1背包)

题干&#xff1a; Given a set of n items, each with a weight w[i] and a value v[i], determine a way to choose the items into a knapsack so that the total weight is less than or equal to a given limit B and the total value is as large as possible. Find the …

服务器2008系统如何设置休眠时间,Win7休眠和睡眠怎么开启(Win2008)

如果把 Win7休眠和睡眠关闭了&#xff0c;需要的时候可以用命令重新开启&#xff0c;毕竟这两个功能不但可以节约电&#xff0c;还可以迅速恢复工作状态&#xff0c;节约开机开软件的时间。Win2008 R2 跟 Win7 同一内核&#xff0c;开启休眠和睡眠的命令也一样。在开启休眠和睡…

【HDU - 1220】Cube (组合数学,简单)

题干&#xff1a; Cowl is good at solving math problems. One day a friend asked him such a question: You are given a cube whose edge length is N, it is cut by the planes that was paralleled to its side planes into N * N * N unit cubes. Two unit cubes may h…

java poi读取word中附件_Java POI导入word, 带图片

1.导入文件示例&#xff0c;word中简历表格模板2.代码示例分两部分&#xff0c;一部分读取图片/*** 导入word(基本信息&#xff0c;word格式)* param staffId* param baseInfoFile*/void importStaffInfo(Integer staffId, MultipartFile file);-- 读取图片InputStream inputSt…

如何将文件拷贝服务器上,如何将文件复制到云服务器上

如何将文件复制到云服务器上 内容精选换一换将文件上传至Windows云服务器一般会采用MSTSC远程桌面连接的方式。本节为您介绍本地Windows计算机通过远程桌面连接&#xff0c;上传文件至Windows云服务器的操作方法。Windows云服务器可以访问公网。在本地Windows计算机上&#xff…

mysql5.7解压版错误_mysql 5.7 解压版 安装net start mysql 发生系统错误 2

1.配置环境变量 用户变量path 添加 mysql 安装目录2.新建my.ini文件 放到E:\mysql-5.7.24-winx64安装目录下[mysqld]port 3306basedirC:/software/mysql-5.7.21-winx64datadirC:/software/mysql-5.7.21-winx64/datamax_connections200character-set-serverutf8default-storage…

【HDU - 2553】N皇后问题 (dfs经典问题,回溯,搜索)

题干&#xff1a; 在N*N的方格棋盘放置了N个皇后&#xff0c;使得它们不相互攻击&#xff08;即任意2个皇后不允许处在同一排&#xff0c;同一列&#xff0c;也不允许处在与棋盘边框成45角的斜线上。 你的任务是&#xff0c;对于给定的N&#xff0c;求出有多少种合法的放置方…

浪潮服务器建立虚拟驱动器,像《十二时辰》一样去建立标准! 浪潮这款服务器做到了...

原标题&#xff1a;像《十二时辰》一样去建立标准&#xff01; 浪潮这款服务器做到了这个夏天&#xff0c;《长安十二时辰》制霸屏幕开画至今豆瓣评分达到8.8分现已成功“出海”在Amazon、Youtube、Viki付费上线成为唐风古韵的又一风向标现如今&#xff0c;越洋的标准可不止悠悠…

【HDU - 5918 】Sequence I (数组(字符串)匹配问题,可选KMP)

题干&#xff1a; Mr. Frog has two sequences a1,a2,⋯,ana1,a2,⋯,an and b1,b2,⋯,bmb1,b2,⋯,bm and a number p. He wants to know the number of positions q such that sequence b1,b2,⋯,bmb1,b2,⋯,bm is exactly the sequence aq,aqp,aq2p,⋯,aq(m−1)paq,aqp,aq2p,…

mysql_fetch_bit_mysql_fetch_array()

mysql_fetch_array()(PHP 4, PHP 5)从结果集中取得一行作为关联数组&#xff0c;或数字数组&#xff0c;或二者兼有说明mysql_fetch_array(resource$result[,int$ result_type]):array返回根据从结果集取得的行生成的数组&#xff0c;如果没有更多行则返回FALSE。mysql_fetch_a…

【HDU - 5916】Harmonic Value Description (构造,思维,SJ题)

题干&#xff1a; The harmonic value of the permutation p1,p2,⋯pnp1,p2,⋯pn is ∑i1n−1gcd(pi.pi1)∑i1n−1gcd(pi.pi1) Mr. Frog is wondering about the permutation whose harmonic value is the strictly k-th smallest among all the permutations of [n]. Inpu…

在python中、对于函数定义代码的理解_python中如何理解装饰器代码?

长文预警&#xff0c;【最浅显易懂的装饰器讲解】能不能专业地复制题目&#xff1f;配上代码&#xff0c;问题分段。我来给提主配上问题的代码。正式回答&#xff1a;1&#xff1a;如何理解return一个函数&#xff0c;它与return一个值得用法区别在哪&#xff1f;敲黑板&#x…

【AtCoder - 2554】Choose Integers (找规律,或枚举)

题干&#xff1a; Problem Statement We ask you to select some number of positive integers, and calculate the sum of them. It is allowed to select as many integers as you like, and as large integers as you wish. You have to follow these, however: each sele…

1m带宽可以做mysql数据库吗_服务器的1M带宽够用吗?1M网速是多少?

1M是什么&#xff1f;通常&#xff0c;在计算机中1M表示的是计算机的内存容量&#xff0c;即1M1024KB。但有不同的含义&#xff0c;云服务器中1M表示服务器的带宽&#xff0c;即1M带宽。在服务器中1M带宽够用吗&#xff1f;云服务器的1兆网速是多少&#xff1f;首先把云服务器带…

【POJ - 2318】TOYS(计算几何,叉积判断点与直线位置关系,二分)

题干&#xff1a; Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a problem - their child John never puts his toys away when he is finished playing with them. They gave John a rectangular box to put his toys i…

esp32 micropython spiffs_spiffs 文件系统在esp32中的应用

spiffs 介绍SPIFFS 是一个开源文件系统&#xff0c;用于 SPI NOR flash 设备的嵌入式文件系统&#xff0c;支持磨损均衡、文件系统一致性检查等功能。spiffs 源码地址​github.comspiffs 特点而我们知道乐鑫的esp32的大部分存储都依赖于SPI flash &#xff0c;spiffs可以说对于…

【HDU - 1968】【UVA - 12096】The SetStack Computer (模拟,集合求交集并集操作,STL实现)

题干&#xff1a; Background from Wikipedia: 揝et theory is a branch of mathematics created principally by the German mathematician Georg Cantor at the end of the 19th century. Initially controversial, set theory has come to play the role of a foundational…

info testing mysql_SQLMASQLMAP中文说明(linux版本)

这个东西&#xff0c;是mickey整理的&#xff0c;不多说了&#xff0c;尊重一下原作者&#xff0c;转载注明mickey整理就好了更新svn checkout https://svn.sqlmap.org/sqlmap/trunk/sqlmap sqlmap-devsqlmap.py -u "http://www.islamichina.com/hotelinchina.asp?cityid…