Charm Bracelet POJ - 3624

博客围绕Charm Bracelet问题展开,Bessie要在重量限制下选魅力值最大的饰品。给出输入输出格式及示例,指出该问题是典型的01背包问题,并给出思路,最后展示了代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 W i (1 ≤ W i ≤ 400), a ‘desirability’ factor D i (1 ≤ D i ≤ 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: W i and D i

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

思路:

赤裸裸的01背包,背就完了

Sample Output

23

思路:

赤裸裸的01背包,背就完了

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<string>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define ll long long
#define mes(x,y); memset(x,y,sizeof(x))
#define mv 2147483648+30
using namespace std;
ll gar(ll a,ll b){//最大公约数 
return b==0?a:gar(b,a%b);
} 
struct node{
	int x,y;
}a[21000];
int dp[21000];
int main(){
	int n,m;
	while(cin>>n>>m){
		mes(a,0);mes(dp,0);
		for(int i=1;i<=n;i++){
			cin>>a[i].x>>a[i].y;
		}
		for(int i=1;i<=n;i++){
			for(int j=m;j>=a[i].x;j--){
				dp[j]=max(dp[j],dp[j-a[i].x]+a[i].y);
			}
		}
		cout<<dp[m]<<endl;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GUESSERR

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值