POJ - 3624 Charm Bracelet (滚动数组/01背包)

本文介绍了一种解决魅力手链问题的动态规划算法,旨在从多种魅力饰品中选择最佳组合,使得总魅力值最大同时不超过重量限制。

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

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int man=1e4+5;
int dp[13000];
int q[3500],d[13000];
int n,m;
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(dp,0,sizeof(dp));
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d",&q[i],&d[i]);
            for(int j=m;j>=0;j--)
            {
                if(j>=q[i])
                    dp[j]=max(dp[j],dp[j-q[i]]+d[i]);
            }
        }
        printf("%d\n",dp[m]);
    }
}



<div class="ptt" style="text-align: center; font-size: 18pt; font-weight: bold; color: blue; padding: 10px;"><a target=_blank href="http://acm.hust.edu.cn/vjudge/problem/17547/origin" target="_blank" style="color: blue; text-decoration: none;">Charm Bracelet</a></div><div class="plm" style="text-align: center;font-size:14px;"><table align="center"><tbody><tr><td><strong>Time Limit:</strong> 1000MS</td><td width="10px"> </td><td><strong>Memory Limit:</strong> 65536KB</td><td width="10px"> </td><td><strong>64bit IO Format:</strong> %lld & %llu</td></tr></tbody></table></div><p class="opt_btn" align="center" style="font-family: Simsun;font-size:14px;"><span style="font-size:14px;color:#333399;"><a target=_blank href="http://acm.hust.edu.cn/vjudge/problem/submit/17547" class="login ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" style="display: inline-block; position: relative; padding: 0px; margin-right: 0.1em; cursor: pointer; vertical-align: middle; overflow: visible; text-decoration: none; font-family: Verdana, Arial, sans-serif; border: 1px solid rgb(211, 211, 211); color: blue; border-radius: 4px; font-size: 12px !important; background: url("../jquery-ui-1.11.1.custom/images/ui-bg_glass_75_e3e4f8_1x400.png") 50% 50% repeat-x rgb(227, 228, 248);"><span class="ui-button-text" style="color: black; display: block; padding: 0.4em 1em;">Submit</span></a> <a target=_blank href="http://acm.hust.edu.cn/vjudge/status/#OJId=POJ&probNum=3624" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" style="display: inline-block; position: relative; padding: 0px; margin-right: 0.1em; cursor: pointer; vertical-align: middle; overflow: visible; text-decoration: none; font-family: Verdana, Arial, sans-serif; border: 1px solid rgb(211, 211, 211); color: blue; border-radius: 4px; font-size: 12px !important; background: url("../jquery-ui-1.11.1.custom/images/ui-bg_glass_75_e3e4f8_1x400.png") 50% 50% repeat-x rgb(227, 228, 248);"><span class="ui-button-text" style="color: black; display: block; padding: 0.4em 1em;">Status</span></a></span></p><div class="hiddable" id="vj_description" style="font-family: "times new roman"; font-size: 17px;"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Description</p><div class="textBG" style="border-radius: 10px; padding: 10px; border-style: dotted; border-width: 2px; background-color: rgb(234, 235, 255);"><div class="ptx" lang="en-US" style="font-family: "Times New Roman", Times, serif; font-size: 12pt;"><p>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 <em>N</em> (1 ≤ <em>N</em> ≤ 3,402) available charms. Each charm <em>i</em> in the supplied list has a weight <em>W<sub>i</sub></em> (1 ≤ <em>W<sub>i</sub></em> ≤ 400), a 'desirability' factor <em>D<sub>i</sub></em> (1 ≤ <em>D<sub>i</sub></em> ≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more than <em>M</em> (1 ≤ <em>M</em> ≤ 12,880).</p><p>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.</p></div></div></div><div class="hiddable" id="vj_input" style="font-family: "times new roman"; font-size: 17px;"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Input</p><div class="textBG" style="border-radius: 10px; padding: 10px; border-style: dotted; border-width: 2px; background-color: rgb(234, 235, 255);"><div class="ptx" lang="en-US" style="font-family: "Times New Roman", Times, serif; font-size: 12pt;"><p>* Line 1: Two space-separated integers: <em>N</em> and <em>M</em>
* Lines 2..<em>N</em>+1: Line <em>i</em>+1 describes charm <em>i</em> with two space-separated integers: <em>W<sub>i</sub></em> and <em>D<sub>i</sub></em></p></div></div></div><div class="hiddable" id="vj_output" style="font-family: "times new roman"; font-size: 17px;"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Output</p><div class="textBG" style="border-radius: 10px; padding: 10px; border-style: dotted; border-width: 2px; background-color: rgb(234, 235, 255);"><div class="ptx" lang="en-US" style="font-family: "Times New Roman", Times, serif; font-size: 12pt;"><p>* Line 1: A single integer that is the greatest sum of charm desirabilities that can be achieved given the weight constraints</p></div></div></div><div class="hiddable" id="vj_sampleInput" style="font-family: "times new roman"; font-size: 17px;"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Sample Input</p><div class="textBG" style="border-radius: 10px; padding: 10px; border-style: dotted; border-width: 2px; background-color: rgb(234, 235, 255);"><pre class="sio" style="white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New", Courier, monospace; font-size: 12pt;">4 6
1 4
2 6
3 12
2 7

Sample Output

23

Source


 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值