POJ 3624 Charm Bracelet(01背包模板题)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 52318 | Accepted: 21912 |
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
Sample Output
AC代码(模板题)
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int N = ;
int dp[N];
int s, n;//背包容积和物品数量 struct Thing
{
int w;
int v;
}list[]; void init()
{
for (int i = ; i <= s; i++)dp[i] = ;
} void package()
{
for (int i = ; i < n; i++)
{
for (int j = s; j >= list[i].w; j--)
{
dp[j] = max(dp[j], dp[j - list[i].w] + list[i].v);
}
}
} int main()
{
scanf("%d%d", &n, &s);
for (int i = ; i < n; i++)scanf("%d%d", &list[i].w, &list[i].v);
init();
package();
printf("%d", dp[s]);
return ;
}
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; struct T
{
int w, v;
}t[]; int main()
{
int n, m;
scanf("%d%d", &n, &m);
for (int i = ; i < n; i++)scanf("%d%d", &t[i].w, &t[i].v);
int dp[];
memset(dp, , sizeof(dp));
for (int i = ; i < n; i++)
{
for (int j = m; j >= t[i].w; j--)dp[j] = max(dp[j - t[i].w] + t[i].v, dp[j]);
}
printf("%d\n", dp[m]);
return ;
}
二刷
POJ 3624 Charm Bracelet(01背包模板题)的更多相关文章
- POJ 3624 Charm Bracelet(01背包裸题)
Charm Bracelet Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38909 Accepted: 16862 ...
- POJ 3624 Charm Bracelet 0-1背包
传送门:http://poj.org/problem?id=3624 题目大意:XXX去珠宝店,她需要N件首饰,能带的首饰总重量不超过M,要求不超过M的情况下,使首饰的魔力值(D)最大. 0-1背包入 ...
- POJ 3624 Charm Bracelet(01背包)
Charm Bracelet Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34532 Accepted: 15301 ...
- poj 3624 Charm Bracelet 01背包问题
题目链接:poj 3624 这是最基础的背包问题,特点是:每种物品仅有一件,可以选择放或不放. 用子问题定义状态:即F [i, v]表示前i件物品恰放入一个容量为v 的背包可以 ...
- 洛谷 P2871 [USACO07DEC]手链Charm Bracelet && 01背包模板
题目传送门 解题思路: 一维解01背包,突然发现博客里没有01背包的板子,补上 AC代码: #include<cstdio> #include<iostream> using ...
- POJ 3624 Charm Bracelet (01背包)
题目链接:http://poj.org/problem?id=3624 Bessie has gone to the mall's jewelry store and spies a charm br ...
- POJ.3624 Charm Bracelet(DP 01背包)
POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...
- HDU 2602 - Bone Collector - [01背包模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Many years ago , in Teddy’s hometown there was a ...
- poj 3624 Charm Bracelet 背包DP
Charm Bracelet Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=3624 Descripti ...
随机推荐
- JavaScript项目总结一
1.类选择其下,第一个 $('selector').first()==$('selector:first')==$('selector:eq(0)') 2.如果要选择非第一个 $('selector: ...
- windows node版本管理工具nvm
有时候,不同的项目需要使用不同的node版本.我们可以使用nvm管理不同的node版本.具体使用方法如下: 1.下载安装nvm 点击我下载nvm 解压下载的文件,双击nvm-setup.exe 直接下 ...
- hdu1010-Tempter of the Bone-(dfs+奇偶剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 翻译:有只狗被困了,S是起点,D是门,W是墙不能走,‘ . ’是可以走的路,走一次就会在1秒内坍塌,也就是 ...
- Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException 拒绝访问 / 出现了内部错误 c# – 当使用X509Certificate2加载p12/pfx文件时出现
环境:iis/netcore 2.2 初始调用:X509Certificate2 certificate = new X509Certificate2(input.Path, CER_PASSWORD ...
- 牛客练习赛55 E 树
题目链接: 题意:给出n个点,n-1条边求任意两个点的距离平方的和 解法: f[i]表示这个点的高度 sz[i]表示这个子树的大小 szz[i]表示这个这个子树大小的平方 sum[i]表示这个子树所有 ...
- node.js – 服务器端的客户端证书验证,DEPTH_ZERO_SELF_SIGNED_CERT错误
我正在使用节点0.10.26并尝试建立与客户端验证的https连接. 服务器代码: var https = require('https'); var fs = require('fs'); proc ...
- idea中隐藏.iml文件
在创建父子工程或者聚合工程时产生的大量 .iml 文件,有时会对我们的操作产生干扰,所以,一般情况下,我们都将其隐藏掉,步骤如下: File——>settings——>Editor——&g ...
- bootstrap中data-*的一些归纳
最近发现date里面的东西跟常用,但是没有过一个系统化的归纳,今天下午正好有小会空,整合一下如下: 1. data-toggle data-toggle指以什么事件类型触发,常用的如下. data-t ...
- Json断言
Additionally assert value:添加验证的值,只有勾选了此复选框,才可以在Expected Value中设置期望的值. Match as regular expression:匹配 ...
- 洛谷 P1972 [SDOI2009]HH的项链-二维偏序+树状数组+读入挂(离线处理,思维,直接1~n一边插入一边查询),hahahahahahaha~
P1972 [SDOI2009]HH的项链 题目背景 无 题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含 ...