{"id":347,"date":"2019-10-07T22:17:55","date_gmt":"2019-10-07T14:17:55","guid":{"rendered":"http:\/\/www.guanhaobo.cn\/?p=347"},"modified":"2019-10-07T22:17:55","modified_gmt":"2019-10-07T14:17:55","slug":"poj-3624-charm-bracelet","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=347","title":{"rendered":"POJ 3624 \u2014 Charm Bracelet"},"content":{"rendered":"<h3>Description<\/h3>\n<p>Bessie has gone to the mall&#8217;s jewelry store and spies a charm bracelet. Of course, she&#8217;d like to fill it with the best charms possible from the N (1 \u2264 N \u2264 3,402) available charms. Each charm i in the supplied list has a weight Wi (1 \u2264 Wi \u2264 400), a &#8216;desirability&#8217; factor Di (1 \u2264 Di \u2264 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more than M (1 \u2264 M \u2264 12,880).<\/p>\n<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>\n<h3>Input<\/h3>\n<ul>\n<li>Line 1: Two space-separated integers: N and M<\/li>\n<li>Lines 2..N+1: Line i+1 describes charm i with two space-separated integers: Wi and Di<\/li>\n<\/ul>\n<h3>Output<\/h3>\n<ul>\n<li>Line 1: A single integer that is the greatest sum of charm desirabilities that can be achieved given the weight constraints<\/li>\n<\/ul>\n<h3>Sample Input<\/h3>\n<pre><code class=\"language-cpp line-numbers\">4 6\n1 4\n2 6\n3 12\n2 7\n<\/code><\/pre>\n<h3>Sample Output<\/h3>\n<p><code>23<\/code><\/p>\n<h3>\u9898\u76ee\u94fe\u63a5<\/h3>\n<p><a href=\"http:\/\/poj.org\/problem?id=3624\" title=\"http:\/\/poj.org\/problem?id=3624\">http:\/\/poj.org\/problem?id=3624<\/a><\/p>\n<h3>\u9898\u76ee\u7ffb\u8bd1<\/h3>\n<p>\u6709  N (1 \u2264 N \u2264 3,402) \u4e2a\u7269\u54c1\uff0c\u91cd\u91cf\u5206\u522b\u4e3a Wi (1 \u2264 Wi \u2264 400)\uff0c\u4ef7\u503c\u5206\u522b\u4e3a Di (1 \u2264 Di \u2264 100)\u3002\u73b0\u5728\u6709\u4e00\u4e2a\u627f\u8f7d\u91cd\u91cf\u6700\u5927\u4e3a  M (1 \u2264 M \u2264 12,880) \u7684\u7bb1\u5b50\uff0c\u6c42\u80fd\u653e\u5165\u7bb1\u5b50\u7684\u7269\u54c1\u7684\u603b\u4ef7\u503c\u6700\u5927\u4e3a\u591a\u5c11\uff1f<\/p>\n<h3>\u9898\u76ee\u5206\u6790<\/h3>\n<p>01\u80cc\u5305\u95ee\u9898\u3002<br \/>\n\u52a8\u6001\u89c4\u5212\u3002<br \/>\n\u8bbe<code>res[i][j]<\/code>\u8868\u793a\u5728\u524d i \u4e2a\u7269\u54c1\u4e2d\u9009\u62e9\u82e5\u5e72\u7269\u54c1\u653e\u5165\u627f\u8f7d\u91cd\u91cf\u4e3a j \u7684\u7bb1\u5b50\u91cc\uff0c\u80fd\u653e\u5165\u7684\u7269\u54c1\u7684\u603b\u4ef7\u503c\u7684\u6700\u5927\u503c\u3002<br \/>\n<code>w[i] &gt; j<\/code>\u65f6\uff0c\u7b2c i \u4e2a\u7269\u54c1\u4e0d\u80fd\u653e\u5165\u627f\u91cd\u4e3a j \u7684\u7bb1\u5b50\u4e2d\uff0c\u6613\u5f97<code>res[i][j] = res[i-1][j]<\/code><br \/>\n<code>w[i] &lt;= j<\/code>\u65f6\uff0c\u5982\u679c\u7b2c i \u4e2a\u7269\u54c1\u4e0d\u653e\u5165\u7bb1\u5b50\uff0c\u5219<code>res[i][j] = res[i-1][j]<\/code>\uff1b\u5982\u679c\u7b2c i \u4e2a\u7269\u54c1\u653e\u5165\u7bb1\u5b50\uff0c\u5219<code>res[i][j] = d[i] + res[i-1][j-w[i]]<\/code>\uff1b\u6240\u4ee5\u6709<code>res[i][j] = max(res[i - 1][j], d[i] + res[i - 1][j - w[i]])<\/code><br \/>\n\u7efc\u4e0a\u6240\u8ff0\uff0c\u6211\u4eec\u5f97\u5230\u4e86\u4e0b\u9762\u7684\u9012\u63a8\u5173\u7cfb\u5f0f<\/p>\n<pre><code class=\"language-cpp line-numbers\">if (j &gt;= w[i])\n    res[i][j] = max(res[i - 1][j], res[i - 1][j - w[i]] + d[i]);\nelse\n    res[i][j] = res[i - 1][j];\n<\/code><\/pre>\n<p>\u6211\u4eec\u4f1a\u53d1\u73b0\uff0c<code>res[i][j]<\/code>\u53ea\u548c\u4e0b\u6807\u6bd4\u5b83\u5c0f\u7684<code>res[i - 1][j]<\/code>\u4ee5\u53ca<code>res[i - 1][j - w[i]]<\/code>\u6709\u5173\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e24\u5c42\u5faa\u73af\u5c06 res \u6570\u7ec4\u6240\u6709\u7684\u503c\u90fd\u8ba1\u7b97\u51fa\u6765\u3002<br \/>\n<code>res[n][m]<\/code>\u5c31\u662f\u95ee\u9898\u7684\u7b54\u6848\u3002<br \/>\n\u4f46\u662f\uff0c\u6211\u4eec\u81f3\u5c11\u9700\u8981\u5f00\u4e00\u4e2a<code>3402 * 12880<\/code>\u8fd9\u4e48\u5927\u7684\u6570\u7ec4\uff0c\u5f88\u660e\u663e\u4f1a\u8d85\u51fa\u5185\u5b58\u9650\u5236\u3002<br \/>\n\u6211\u4eec\u53d1\u73b0\uff0c<code>res[i][j]<\/code>\u53ea\u548c<code>res[i-1]<\/code>\u6709\u5173\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u5c06<code>res<\/code>\u6570\u7ec4\u7684\u7b2c\u4e00\u7ef4\u53bb\u6389\u3002\u5728<code>j &lt; w[i]<\/code>\u65f6\u7684<code>res[i][j] = res[i - 1][j]<\/code>\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4e0d\u5199\u4e86\u3002<br \/>\n<code>res[m]<\/code>\u5c31\u662f\u7b54\u6848\u3002<\/p>\n<pre><code class=\"language-cpp line-numbers\">res[j] = max(res[j], res[j - w[i]] + d[i]);\n<\/code><\/pre>\n<p>\u4f46\u8981\u6ce8\u610f\uff0c\u56e0\u4e3a<code>res[j]<\/code>\u4f1a\u53d7\u5230<code>res[j - w[i]]<\/code>\u7684\u5f71\u54cd\uff0c\u6240\u4ee5\u5185\u5c42\u5faa\u73af\u9700\u8981\u7531\u5927\u5230\u5c0f\u8fdb\u884c\u3002<\/p>\n<h3>AC\u4ee3\u7801<\/h3>\n<pre><code class=\"language-cpp line-numbers\">#include &lt;iostream&gt;\nusing namespace std;\nint res[13000] = {0}, w[3500], d[3500];\nint main()\n{\n    int i, j, n, m;\n    cin &gt;&gt; n &gt;&gt; m;\n    for (i = 1; i &lt;= n; i++)\n        cin &gt;&gt; w[i] &gt;&gt; d[i];\n    for (i = 1; i &lt;= n; i++)\n        for (j = m; j &gt;= w[i]; j--)\n            res[j] = max(res[j], res[j - w[i]] + d[i]);\n    cout &lt;&lt; res[m] &lt;&lt; endl;\n    return 0;\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Description Bessie has gone to the mall&#8217;s jewelry [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[25,40],"class_list":["post-347","post","type-post","status-publish","format-standard","hentry","category-algo","tag-poj","tag-40"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/347","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=347"}],"version-history":[{"count":0,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/347\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}