Charm Bracelet-POJ3624(01背包)
http://poj.org/problem?id=3624
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 29444 | Accepted: 13198 |
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 这个是简单的01背包 从我开始接触到背包到现在我还是不懂背包的原理
开始背包的旅程
正常的背包是
i 1....n
j m....0
dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i]]+v[i]);
但是这个范围较大
如果用二维的会超内存
可以转化成一维的
dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<iostream> using namespace std; #define N 3500
int dp[]; int main()
{
int n,m,w[N],v[N];
while(scanf("%d %d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++)
scanf("%d %d",&w[i],&v[i]);
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
{
for(int j=m;j>=;j--)
{
if(j>=w[i])
dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
}
}
printf("%d\n",dp[m]);
}
return ;
}
Charm Bracelet-POJ3624(01背包)的更多相关文章
- POJ.3624 Charm Bracelet(DP 01背包)
POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...
- POJ3624 Charm Bracelet 【01背包】
Charm Bracelet Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22621 Accepted: 10157 ...
- Poj3624 Charm Bracelet (01背包)
题目链接:http://poj.org/problem?id=3624 Description Bessie has gone to the mall's jewelry store and spie ...
- poj 3524 Charm Bracelet(01背包)
Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd ...
- 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(01背包模板)
Charm Bracelet Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45191 Accepted: 19318 ...
- Charm Bracelet(01背包)
Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fil ...
- poj 3624 Charm Bracelet(01背包)
Charm Bracelet Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29295 Accepted: 13143 ...
- P2871 [USACO07DEC]手链Charm Bracelet(01背包模板)
题目传送门:P2871 [USACO07DEC]手链Charm Bracelet 题目描述 Bessie has gone to the mall's jewelry store and spies ...
- POJ 3624 Charm Bracelet 简单01背包
题目大意:有n件珠宝,每个珠宝的魅力值为v,重量为w,求在重量不超过m的情况下能达到的最大魅力值. 题目思路:简单的01背包,由于二维数组会超内存所以应该压缩成一维数组. dp[i][j],表示选取i ...
随机推荐
- AJPFX总结JAVA基本数据类型
1:关键字(掌握) (1)被Java语言赋予特定含义的单词 (2)特点: 全部小写. (3)注意事项: ...
- C#过时方法标记
1.当遇到过时或废弃的方式 函数怎么办 [Obsolete]特性解决你的困惑 1.1:当方法已经完成相关兼容 可以保留时
- Objective - c Foundation 框架详解2
Objective - c Foundation 框架详解2 Collection Agency Cocoa provides a number of collection classes such ...
- iOS中的蓝牙
iOS中的蓝牙 概述 iOS中提供了4个框架用于实现蓝牙连接 1.GameKit.framework(用法简单) 只能用于iOS设备之间的同个应用内连接,多用于游戏(eg.拳皇,棋牌类),从iOS7开 ...
- this常用的用法
1.函数作为对象的方法时,this指的是该对象: var obj ={ name:"bob", age:25, getName:function(){ console.log(th ...
- PHP文件及目录考察点
文件读取/写入操作 fopen()函数 用来打开一个文件,打开时需要指定打开模式 打开模式 模式 |作用 --- |--- 'r' |只读方式打开,将文件指针指向文件头. 'r+' |读写方式打开,将 ...
- Android学习——蓝牙通讯
蓝牙蓝牙,是一种支持设备短距离通信(一般10m内,且无阻隔媒介)的无线电技术.能在包括移动电话.PDA.无线耳机.笔记本电脑等众多设备之间进行无线信息交换.利用“蓝牙”技术,能够有效的简化移动通信终端 ...
- Java入门第39课——猜字母游戏之实现字母生成方法
问题 实现猜字母游戏中的字母生成方法,即,随机生成5个不同的字母作为猜测的结果. 方案 实现generate方法,首先声明一个字符类型的数组,用于存储26个大写字母,然后声 ...
- spring cloud 概念
微服务构架需要使用场景: 1.可以将一个系统拆分成几个系统. 2.每个子系统可以部署多个应用,多个应用之间可以使用负载均衡. 3.需要一个服务注册中心,所有的服务都在一个注册中心注册,负载均衡也是通过 ...
- java正则表达式进阶运用20181023
直接上代码. package org.jimmy.autotranslate20181022.test; import java.util.regex.Matcher; import java.uti ...