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 ...
随机推荐
- 网络I/O
贴几个超级不错的博客 1.Linux IO模式及 select.poll.epoll详解 2.网络 I/O 模型 3.同步异步阻塞非阻塞 4.三种模式的区别与联系
- web万维网 -- 基础概念
Web(万维网World Wide Web的简称)是个包罗万象的万花筒,不同的人从不同的角度观察,对于Web究竟是什么会得出大不相同的观点. 百科:web(World Wide Web)即全球广域网, ...
- v-for给img的src动态赋值问题
做一个轮播图,给img赋值src <el-carousel-item v-for="(item, index) in carouselImgs" :key="ind ...
- 27.SpringBoot和SpringMVC的区别
所以,用最简练的语言概括就是: Spring 是一个“引擎”: Spring MVC 是基于Spring的一个 MVC 框架: Spring Boot 是基于Spring4的条件注册的一套快速开发整合 ...
- Socket内核调用数SYSCALL_DEFINE3
http://blog.chinaunix.net/uid-20788636-id-4408261.html 前言: 对于Linux内核的Socket系列文章都是依据于:Linux-3.14.5的版本 ...
- dotnet core linux 接入支付宝H5支付,提示:System.PlatformNotSupportedException","Message":"'CspParameters' requires Windows Cryptographic API (CAPI), which is not available on this platform.
用的官方提供的demo,实际上部署后却出现了上图的错误.和技术支持沟通无效后,走上了不归路. 在微软的github dotnet/core开源库提交了issue后,终于获得了解决.附上链接:https ...
- PostgreSQL 执行计划
简介 PostgreSQL是“世界上最先进的开源关系型数据库”.因为出现较晚,所以客户人群基数较MySQL少,但是发展势头很猛,最大优势是完全开源. MySQL是“世界上最流行的开源关系型数据库”.当 ...
- python函数 | 列表生成式
在编写程序或者查看别人的程序时,经常会遇到列表生成式,这个使用起来并不复杂,但是非常有用,使我们的代码更加简洁灵活.很多python使用者并不太会使用它.今天,就给大家详细讲解列表生成式和生成器表达式 ...
- ent 基本使用十二 字段
字段或者属性,在schema中是定点的属性,比如user 包含4个字段age,name,username,created_at 图表展示如下: 代码描述 package schema impo ...
- netflix flamescope 方便的不同时间范围的火焰图查看工具
flamescope 是netflix 开源的方便的火焰图查看工具,我们可以选择不同时间范围的数据,方便分析调用链 环境准备 使用docker-compose运行,基于官方的dockerfile 创建 ...