{"id":245,"date":"2020-04-13T23:56:14","date_gmt":"2020-04-13T15:56:14","guid":{"rendered":"http:\/\/47.103.223.234:100\/?p=245"},"modified":"2020-04-15T23:21:11","modified_gmt":"2020-04-15T15:21:11","slug":"%e8%be%83%e7%ae%80%e5%8d%95%e7%9a%84%e5%87%a0%e7%a7%8d%e8%83%8c%e5%8c%85%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/www.zyhcoding.club\/index.php\/2020\/04\/13\/%e8%be%83%e7%ae%80%e5%8d%95%e7%9a%84%e5%87%a0%e7%a7%8d%e8%83%8c%e5%8c%85%e9%97%ae%e9%a2%98\/","title":{"rendered":"\u8f83\u7b80\u5355\u7684\u51e0\u79cd\u80cc\u5305\u95ee\u9898"},"content":{"rendered":"<p>\u4ecb\u7ecd01\u80cc\u5305\u3001\u591a\u91cd\u80cc\u5305\u3001\u5b8c\u5168\u80cc\u5305\u7b49\u3002\u8fd9\u51e0\u79cd\u7b80\u5355\u57fa\u7840\u7684\u80cc\u5305\u3002\u9ebb\u70e6\u7684\u57fa\u672c\u90fd\u662f\u5b83\u4eec\u7684\u5ef6\u4f38\u3002<\/p>\n<p>01\u80cc\u5305 \u4e8c\u7ef4\u6570\u7ec4\u7684\u5751\uff01\uff01\u4e00\u7ef4\u6570\u7ec4\u7684\u4fbf\u5229<\/p>\n<h3>01\u80cc\u5305<\/h3>\n<p><a href=\"http:\/\/acm.hdu.edu.cn\/showproblem.php?pid=2602\">\u4f8b\u9898 HDU2602 \u6361\u9aa8\u5934<\/a><br \/>\n\u6709\u4e00\u4e2a\u5927\u5c0f\u4e3aM\u7684\u80cc\u5305\uff0c\u6709n\u4ef6\u7269\u54c1\uff0c\u6bcf\u4ef6\u7269\u54c1\u90fd\u6709\u4e00\u4e2a\u91cd\u91cfw\u548c\u4e00\u4e2a\u4ef7\u503cv\uff0c\u5728n\u4ef6\u7269\u54c1\u4e2d\u62ff\u51fa\u51e0\u4ef6\u653e\u5728\u80cc\u5305\u91cc\uff0c\u8981\u6c42\u5728\u6ee1\u8db3\u91cd\u91cf\u4e4b\u548c\u5c0f\u4e8eM\u7684\u60c5\u51b5\u4e0b\uff0c\u7269\u54c1\u7684\u4ef7\u503c\u4e4b\u548c\u6700\u5927\u3002\u6bcf\u4ef6\u7269\u54c1\u90fd\u53ea\u80fd\u62ff\u81f3\u591a\u4e00\u6b21\u3002<br \/>\n\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff1a<\/p>\n<blockquote>\n<p><strong>dp[i][j] = max(dp[i-1][j],dp[i-1][j-w[i]]+v[i])<\/strong><\/p>\n<\/blockquote>\n<p>\u5206\u6790\u65b9\u7a0b\uff0cdp[i][j]\u8868\u793a\u5728\u53ea\u8003\u8651\u524di\u4ef6\u7269\u54c1\u7684\u60c5\u51b5\u4e0b\uff0c\u80cc\u5305\u5927\u5c0f\u4e3aj\u65f6\u7684\u6700\u4f18\u89e3\u3002\u5728\u65b0\u52a0\u5165\u7269\u54c1a[i]\u65f6\uff0c\u8fd9\u4ef6\u7269\u54c1\uff0c\u8981\u4e48\u62ff\uff0c\u8981\u4e48\u4e0d\u62ff\uff0c\u5728\u8fd9\u4e24\u79cd\u60c5\u51b5\u91cc\u9009\u6700\u4f18\u3002\u4e0d\u62ff\u7684\u8bdd\uff0c\u6700\u4f18\u89e3\u4e3adp[i-1][j]\uff1b\u62ff\u7684\u8bdd\uff0c\u6700\u4f18\u89e3\u4e3adp[i-1][j-w[i]]+v[i]\u3002<\/p>\n<h5>\u4e8c\u7ef4\u6570\u7ec4\u4f5c\u6cd5<\/h5>\n<pre><code class=\"language-c++\">for (int i=1;i&lt;=n;i++){\n    for (int j=1;j&lt;=M;j++){\n        if (a[i]&gt;j)\n            dp[i][j] = dp[i-1][j];\n        else\n            dp[i][j] = max(dp[i-1][j],dp[i-1][j-w[i]]+v[i]);\n    }\n}<\/code><\/pre>\n<p>\u8e29\u8fc7\u7684\u5751\uff1a\u4e8c\u7ef4\u6570\u7ec4\u4e0d\u540c\u4e8e\u6eda\u52a8\u6570\u7ec4\uff0c\u4e00\u5b9a\u8981\u628aa[i]&lt;j\u7684\u60c5\u51b5\u4e0b\u7684\u6570\u7ec4\u4e5f\u66f4\u65b0\u4e00\u4e0b\uff01   \u5426\u5219\u4f1a\u51fa\u9519\u3002\u3002<br \/>\n\u3002<\/p>\n<h5>\u6eda\u52a8\u6570\u7ec4\u4f5c\u6cd5<\/h5>\n<p>\u6709\u65f6\u5019NM\u7684\u503c\u5f88\u5927\u65f6\uff0c\u5bfc\u81f4\u65e0\u6cd5\u5f00\u51fa\u8fd9\u4e48\u5927\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u8fd9\u65f6\u5019\u5c31\u8981\u7528\u5230\u6eda\u52a8\u6570\u7ec4\u4e86\u3002dp[M].<br \/>\ndp[j]\u7684\u503c\u8868\u793a\u5728\u5df2\u8003\u8651\u8fc7\u7684\u524di\u4ef6\u7269\u54c1\u4e2d\uff0c\u80cc\u5305\u5927\u5c0f\u4e3aj\u65f6\u7684\u6700\u4f18\u89e3\u3002<\/p>\n<pre><code class=\"language-c++\">for (int i=1;i&lt;=n;i++){\n    for (int j=M;j&gt;=a[i];j--){\n        dp[j] = max(dp[j],dp[j-a[i]]+v[i]);\n    }\n}<\/code><\/pre>\n<p>\u6ce8\u610f\u6eda\u52a8\u6570\u7ec4\u4e2d\u80cc\u5305\u5927\u5c0f\u7684\u503cj\u4e00\u5b9a\u8981\u4ece\u5927\u5230\u5c0f\u5faa\u73af\uff0c\u56e0\u4e3a\u4ece\u5927\u5230\u5c0f\u7684\u8bdd\uff0c\u5bf9\u4e8ek&lt;j\uff0cdp[k]\uff0c\u4e5f\u5c31\u662f\u4ee3\u7801\u4e2d\u7684dp[j-a[i]]\uff0c\u662f\u524di-1\u4ef6\u7269\u54c1\u7684\u6700\u4f18\u89e3\u3002\u5982\u679c\u4ece\u5c0f\u5230\u5927\u5faa\u73af\uff0cdp[k]\u662f\u5df2\u7ecf\u5305\u542b\u53ef\u7b2ci\u4ef6\u7269\u54c1\u7684\u6700\u4f18\u89e3\uff0c\u4e0d\u80fd\u518d\u6b21\u8003\u8651\uff0c\u5426\u5219dp[j-a[i]]\u8fd9\u4e2a\u89e3\u53ef\u80fd\u5df2\u7ecf\u628aa[i]\u5305\u542b\u4e86\u4e00\u6b21\u4e86\u3002<\/p>\n<h3>\u5b8c\u5168\u80cc\u5305<\/h3>\n<p><a href=\"https:\/\/www.luogu.com.cn\/problem\/P1048\">\u91c7\u836f \u6d1b\u8c37P1048<\/a><br \/>\n\u5b8c\u5168\u80cc\u5305\uff0c\u5c31\u662f\u572801\u80cc\u5305\u7684\u57fa\u7840\u4e0a\uff0c\u6bcf\u4ef6\u7269\u54c1\u53ef\u4ee5\u62ff\u65e0\u9650\u6b21\u3002<br \/>\n\u53ea\u9700\u572801\u80cc\u5305\u7684\u65b9\u7a0b\u4e0a\u5c0f\u505a\u6539\u52a8\u5373\u53ef\u3002<\/p>\n<blockquote>\n<p><strong>dp[i][j] = max(dp[i][j],dp[i][j-w[i]]+v[i])<\/strong><\/p>\n<\/blockquote>\n<p>\u628ai-1\u53d8\u6210i\u5c31\u884c\u4e86\u3002i-1\u7684\u542b\u4e49\u662f\u7b2ci\u4ef6\u7269\u54c1\u62ff\u4e86\u5c31\u4e0d\u80fd\u518d\u62ff\u4e86\uff0c\u53ea\u80fd\u8003\u8651\u524di-1\u4ef6\u3002\u90a3\u73b0\u5728\u6bcf\u4ef6\u7269\u54c1\u53ef\u4ee5\u62ff\u65e0\u9650\u6b21\uff0ci-1\u6539\u6210i\u5c31ok\u4e86\u3002<\/p>\n<h5>\u4e8c\u7ef4\u6570\u7ec4\u4f5c\u6cd5<\/h5>\n<pre><code class=\"language-c++\">for (int i=1;i&lt;=n;i++){\n    for (int j=1;j&lt;=M;j++){\n        if (a[i]&gt;j)\n            dp[i][j] = dp[i-1][j];   \/\/\u8fd9\u91cc\u6ce8\u610f\u4e0b\u5982\u679cj&lt;a[i],\u4e5f\u5c31\u662f\u7b2ci\u4ef6\u7269\u54c11\u4ef6\u90fd\u62ff\u4e0d\u4e0b\u65f6\uff0c\u53ea\u80fd\u8003\u8651\u524di-1\u4ef6\u3002\n        else\n            dp[i][j] = max(dp[i][j],dp[i][j-w[i]]+v[i]);\n    }\n}<\/code><\/pre>\n<h5>\u6eda\u52a8\u6570\u7ec4\u4f5c\u6cd5<\/h5>\n<p>\u628a01\u80cc\u5305\u7684\u4ee3\u7801\u4e2d\u5185\u5c42\u5faa\u73af\u4ece\u5927\u5230\u5c0f\u904d\u5386\uff0c\u6539\u6210\u4ece\u5c0f\u5230\u5927\u904d\u5386\u5373\u53ef\u3002<br \/>\n\u4ece\u5c0f\u5230\u5927\u904d\u5386\uff0c\u5c31\u662f\u8bf4\u8fd9\u4ef6\u7269\u54c1\u5df2\u7ecf\u62ff\u8fc7\u4e86\uff0c\u8fd8\u53ef\u4ee5\u518d\u62ff\u7684\u3002<\/p>\n<pre><code class=\"language-c++\">for (int i=1;i&lt;=n;i++){\n    for (int j=a[i];j&lt;=M;j++){\n        dp[j] = max(dp[j],dp[j-a[i]]+v[i]);\n    }\n}<\/code><\/pre>\n<h3>\u591a\u91cd\u80cc\u5305<\/h3>\n<p><a href=\"http:\/\/acm.hdu.edu.cn\/showproblem.php?pid=2844\">\u786c\u5e01 HDU2844<\/a><br \/>\n\u8fd9\u4e2a\u5012\u662f~\u6709\u70b9\u590d\u6742\u7684~\uff0c\u572801\u80cc\u5305\u7684\u57fa\u7840\u4e0a\uff0c\u6bcf\u4ef6\u7269\u54c1\u62ff\u7684\u6b21\u6570\u662f\u6709\u9650\u7684\uff0c\u9650\u5236\u4e0d\u518d\u662f1\uff0c\u4e5f\u4e0d\u662f\u65e0\u9650\u6b21\uff0c\u800c\u662f\u6709\u4e2a\u6709\u7a77\u503ck\u3002<br \/>\n\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e5f\u53ef\u4ee5\u8f6c\u6362\u621001\u80cc\u5305\u6765\u89e3\u51b3\uff0c<strong>\u628a\u8fd9\u4e2a\u6700\u591a\u80fd\u62ffk\u6b21\u7684\u4e00\u4ef6\u7269\u54c1\uff0c\u5206\u89e3\u6210\u6700\u591a\u80fd\u62ff1\u6b21\u7684k\u4ef6<\/strong>\u4e0d\u5c31\u884c\u4e86\u3002<br \/>\n\u4f46\u662f\u5f53k\u4e4b\u548c\u5f88\u5927\u65f6\uff0c\u8fd9\u6837\u5c31\u4e0d\u73b0\u5b9e\u4e86\uff0c\u4f1a\u5bfc\u81f4\u6570\u636e\u91cf\u5f88\u5927\u3002<br \/>\n\u8fd9\u91cc\u5e94\u7528\u5230\u4e86\u4e00\u4e2a\u6570\u8bba\u4e2d\u7684\u5c0f\u77e5\u8bc6\uff1a<code class=\"katex-inline\">2^0,2^1,\u2026\u2026,2^n<\/code>\u8fd9\u4e9b\u6570\u53ef\u4ee5\u7ec4\u5408\u6210<code class=\"katex-inline\">2^{n+1}-1<\/code>\u5185\u7684\u4efb\u610f\u4e00\u4e2a\u6570\u3002<br \/>\n\u90a3\u4e48\u628ak\u5206\u89e3\u6210<code class=\"katex-inline\">2^0,2^1,\u2026\u2026,2^n,k-2^n<\/code>\u5c31\u884c\u4e86\uff0c\u5927\u5927\u7f29\u51cf\u4e86\u6570\u636e\u91cf\uff0ck-&gt;logk\u3002<code class=\"katex-inline\">2^{n+1}-1<=k<2^{n+2}-1<\/code><\/p>\n<h3>\u6df7\u5408\u80cc\u5305<\/h3>\n<p>\u5c31\u662f\u4e09\u79cd\u80cc\u5305\u7684\u6df7\u5408\uff0c\u5728\u5b8c\u5168\u638c\u63e1\u4e0a\u8ff0\u4e09\u79cd\u80cc\u5305\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0\uff0c\u4e00\u4ef6\u7269\u54c1\uff0c\u4ed6\u62ff\u7684\u6b21\u6570\u6700\u7ec8\u53ef\u4ee5\u5316\u7b80\u4e3a\u81f3\u591a\u62ff1\u6b21\u3001\u53ef\u4ee5\u62ff\u65e0\u6570\u6b21\uff0c\u5c31\u8fd9\u4e24\u79cd\u60c5\u51b5\uff0c\u7136\u540e\u5bf9\u4e8e\u8fd9\u4e24\u79cd\u60c5\u51b5\u7684\u5faa\u73af\u904d\u5386\u505a\u4e2a\u533a\u5206\u5c31\u884c\u4e86\u3002<br \/>\nvis[i]=1\u65f6\u8868\u793a\u53ef\u4ee5\u62ff\u65e0\u9650\u6b21\uff0cvis[i]=0\u65f6\u8868\u793a\u53ef\u4ee5\u62ff1\u6b21\u3002<br \/>\n\u4e8c\u7ef4\u6570\u7ec4<\/p>\n<pre><code class=\"language-c++\">for (int i=1;i&lt;=n;i++){\n    if (vis[i]){\n        for (int j=1;j&lt;=M;j++){\n            if (a[i]&gt;j)  dp[i][j] = dp[i-1][j];\n            else    dp[i][j] = max(dp[i-1][j],dp[i][j-w[i]]+v[i]);\n        }\n    }else{\n        for (int j=1;j&lt;=M;j++){\n            if (a[i]&gt;j)  dp[i][j] = dp[i-1][j];\n            else    dp[i][j] = max(dp[i-1][j],dp[i-1][j-w[i]]+v[i]);\n        }\n    }\n}<\/code><\/pre>\n<p>\u6eda\u52a8\u6570\u7ec4<br \/>\n\u5c31\u662f\u6eda\u52a8\u65b9\u5f0f\u53d8\u4e86\u4e00\u4e0b\u3002<\/p>\n<pre><code class=\"language-c++\">for (int i=1;i&lt;=n;i++){\n    if (vis[i]){\n        for (int j=w[i];j&lt;=M;j++)\n            dp[j] = max(dp[j],dp[j-w[i]]+v[i]);\n    }else{\n        for (int j=M;j&gt;=w[i];j--)\n            dp[j] = max(dp[j],dp[j-w[i]]+v[i]);\n    }\n}<\/code><\/pre>\n<p>\u4f8b\u9898<br \/>\n<a href=\"http:\/\/acm.hdu.edu.cn\/showproblem.php?pid=2602\">\u4f8b\u9898 HDU2602 \u6361\u9aa8\u5934<\/a>  01\u80cc\u5305<br \/>\n<a href=\"https:\/\/www.luogu.com.cn\/problem\/P1048\">\u91c7\u836f \u6d1b\u8c37P1048<\/a>  \u5b8c\u5168\u80cc\u5305<br \/>\n<a href=\"http:\/\/acm.hdu.edu.cn\/showproblem.php?pid=2844\">\u786c\u5e01 HDU2844<\/a> \u591a\u91cd\u80cc\u5305<\/p>\n<p><a href=\"http:\/\/acm.hdu.edu.cn\/showproblem.php?pid=2844\">\u786c\u5e01 HDU2844<\/a><br \/>\n\u6709\u5404\u79cd\u9762\u503c\u7684\u786c\u5e01\u82e5\u5e72\u4e2a\uff0c\u95ee\u80fd\u7ec4\u6210\u591a\u5c11\u4e2a\u5c0f\u4e8eM\u7684\u6574\u6570\u503c\u3002<br \/>\n\u8f6c\u5316\u621001\u80cc\u5305\u540e\uff0c\u5224\u65addp[N][j]\u662f\u5426\u7b49\u4e8ej\u5373\u53ef\u3002<\/p>\n<pre><code class=\"language-c++\">#include &lt;cstdio&gt;\n#include &lt;cstring&gt;\n#include &lt;algorithm&gt;\n#include &lt;iostream&gt;\nusing namespace std;\nint dp[100005];\nint a[105],b[105],v[10000];\nint main(){\n    int n,m,cnt,ans;\n    while (~scanf (&quot;%d %d&quot;,&amp;n,&amp;m) &amp;&amp; n){\n        cnt = ans = 0;\n        \/\/memset(dp,0,sizeof dp);\n        \/\/memset(v,0,sizeof v);\n        for (int i=1;i&lt;=n;i++)  scanf (&quot;%d&quot;,&amp;a[i]);\n        for (int i=1;i&lt;=n;i++)  scanf (&quot;%d&quot;,&amp;b[i]);\n        for (int i=0;i&lt;=m;i++)  dp[i] = 0;\n        for (int i=1;i&lt;=n;i++){\n            int j;\n            for (j=1;(j&lt;&lt;1)-1&lt;=b[i];j&lt;&lt;=1)\n                v[++cnt] = a[i] * j;\n            j &gt;&gt;= 1;\n            if (b[i]&gt;2*j-1)     v[++cnt] = a[i] * (b[i]-2*j+1);\n        }\n        for (int i=1;i&lt;=cnt;i++)    for (int j=m;j&gt;=v[i];j--)\n            dp[j] = max(dp[j],dp[j-v[i]]+v[i]);\n        for (int i=1;i&lt;=m;i++)  if (dp[i]==i)\n            ans++;\n        printf (&quot;%d\\n&quot;,ans);\n    }\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4ecb\u7ecd01\u80cc\u5305\u3001\u591a\u91cd\u80cc\u5305\u3001\u5b8c\u5168\u80cc\u5305\u7b49\u3002\u8fd9\u51e0\u79cd\u7b80\u5355\u57fa\u7840\u7684\u80cc\u5305\u3002\u9ebb\u70e6\u7684\u57fa\u672c\u90fd\u662f\u5b83\u4eec\u7684\u5ef6\u4f38\u3002 01\u80cc\u5305 \u4e8c\u7ef4 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[24],"tags":[],"_links":{"self":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/245"}],"collection":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/comments?post=245"}],"version-history":[{"count":9,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/245\/revisions"}],"predecessor-version":[{"id":303,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/245\/revisions\/303"}],"wp:attachment":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/media?parent=245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/categories?post=245"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/tags?post=245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}