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

来源:互联网 发布:王者荣耀安卓数据迁移 编辑:程序博客网 时间:2024/05/01 16:31
#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 61 42 63 122 7

Sample Output

23

Source

USACO 2007 December Silver


0 0
原创粉丝点击