POJ-3624 01背包入门
还是入门题,只不过需要优化一下空间,不然就会内存超限
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 DiOutput
* 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 7Sample Output
23
1 #include<iostream>
2 #include<stdio.h>
3 #include<algorithm>
4 #include<string.h>
5 using namespace std;
6 int dp[15000];
7 int main()
8 {
9 int n,w,c,val;
10 cin>>n>>w;
11 for(int i=1;i<=n;i++)
12 {
13 scanf("%d%d",&c,&val);
14 for(int j=w;j>=c;j--)
15 {
16 dp[j]=max(dp[j],dp[j-c]+val);
17 }
18 }
19 cout<<dp[w]<<endl;
20 }
POJ-3624 01背包入门的更多相关文章
- POJ 3624 01背包
初学DP,用贪心的思想想解题,可是想了一个多小时还是想不出. //在max中的两个参数f[k], 和f[k-weight[i]]+value[i]都是表示在背包容量为k时的最大价值 //f[k]是这个 ...
- poj 2184 01背包变形【背包dp】
POJ 2184 Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14657 Accepte ...
- POJ 2184 01背包+负数处理
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10200 Accepted: 3977 D ...
- poj 1837 01背包
Balance Time Limit: 1000 MS Memory Limit: 30000 KB 64-bit integer IO format: %I64d , %I64u Java clas ...
- POJ 3628 01背包 OR 状压
思路: 1.01背包 先找到所有奶牛身高和与B的差. 然后做一次01背包即可 01背包的容积和价格就是奶牛们身高. 最后差值一减输出结果就大功告成啦! 2. 搜索 这思路很明了吧... 搜索的确可以过 ...
- Proud Merchants(POJ 3466 01背包+排序)
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
- 01背包入门 dp
题目引入: 有n个重量和价值分别为Wi,Vi的物品.从这些物品中挑选出总重量不超过W的物品,求所有挑选方案中的价值总和的最大值. 分析: 首先,我们用最普通的方法,针对每个物品是否放入背包进行搜索. ...
- POJ之01背包系列
poj3624 Charm Bracelet 模板题 没有要求填满,所以初始化为0就行 #include<cstdio> #include<iostream> using na ...
- POJ 3624 Charm Bracelet 0-1背包
传送门:http://poj.org/problem?id=3624 题目大意:XXX去珠宝店,她需要N件首饰,能带的首饰总重量不超过M,要求不超过M的情况下,使首饰的魔力值(D)最大. 0-1背包入 ...
- HDU2602 Bone Collector 【01背包】
Bone Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
随机推荐
- 树状数组复习 leetcode 307
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- redhat7 team bonding 双网卡绑定 主备 负载均衡
team简介 team也被称为网络组,是将多个网卡聚合在一起,从而实现冗错和提高吞吐量.适用于redhat7.0以上版本,至多可支持8块网卡.team相对于之前的bonding技术,能提供更好的性能和 ...
- 请大家一定不要像我们公司这样打印log日志
前言 最近接手了公司另一个项目,熟悉业务和代码苦不堪言. 我接手一个新项目,有个习惯,就是看结构,看数据库,搜代码. 其中搜代码是我个人这些年不知不觉形成的癖好,我下面给大家展示下这个小癖好. 正文 ...
- 现代 CSS 解决方案:原生嵌套(Nesting)
很早之前,就写过一篇与原生嵌套相关的文章 -- CSS 即将支持嵌套,SASS/LESS 等预处理器已无用武之地?,彼时 CSS 原生嵌套还处于工作草案 Working Draft (WD) 阶段,而 ...
- 在阿里云和腾讯云的轻量应用服务器上搭建Hadoop集群
引入 本文在两台2核2g的云服务器上搭建了Hadoop集群,两台云服务器分别是阿里云(hjm)和腾讯云(gyt),集群部署规划如下: hjm gyt HDFS NameNode\SecondaryNa ...
- mpi转以太网连接200plc通信不上实际问题和解决方法
西门子S7200plc通信不上实际问题和解决方法 现场通信的同学在现场调试的时候,现在特别是做项目改造的项目,西门子S7200plc通信面临淘汰,但是在改造的项目中还能经常看到他们的身影,下面我们就来 ...
- Apache(2.4.49 2.4.50)--目录遍历--命令执行--(CVE-2021-42013)&&(CVE-2021-41773)
Apache(2.4.49 2.4.50)--目录遍历--命令执行--(CVE-2021-42013)&&(CVE-2021-41773) 复现环境 采用Vulfocus靶场环境进行复 ...
- 树莓派3B/3B+的串口使用
树莓派包含两个串口,一个称之为硬件串口(/dev/ttyAMA0),一个称之为mini串口(/dev/ttyS0).硬件串口由硬件实现,有单独的波特率时钟源,性能高.可靠.mini串口时钟源是由CPU ...
- c语言代码练习10
//判断输入的数字是否为素数#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int main() { int n = 0; int ...
- 【算法】国庆加班,火锅与Linq.AddRange的奇妙螺旋
在国庆假期的一个傍晚,小悦正在家中享受火锅美食.她嘴里咀嚼着鲜嫩的牛肉,脸上洋溢着满足的微笑.突然,手机铃声响起,打破了这温馨的氛围.她拿起手机一看,是公司打来的电话. "小悦,有个紧急的项 ...