{"id":226,"date":"2020-04-13T22:15:36","date_gmt":"2020-04-13T14:15:36","guid":{"rendered":"http:\/\/47.103.223.234:100\/?p=226"},"modified":"2020-04-13T23:44:10","modified_gmt":"2020-04-13T15:44:10","slug":"%e5%8a%a8%e6%80%81%e8%a7%84%e5%88%92%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"http:\/\/www.zyhcoding.club\/index.php\/2020\/04\/13\/%e5%8a%a8%e6%80%81%e8%a7%84%e5%88%92%e4%bb%8b%e7%bb%8d\/","title":{"rendered":"\u52a8\u6001\u89c4\u5212\u4ecb\u7ecd"},"content":{"rendered":"<h2>\u52a8\u6001\u89c4\u5212(Dynamic Programming)<\/h2>\n<p>\u52a8\u6001\u89c4\u5212\u95ee\u9898\u4e00\u76f4\u662f\u7b97\u6cd5\u9762\u8bd5\u5f53\u4e2d\u7684\u91cd\u70b9\u548c\u96be\u70b9\uff0c\u5e76\u4e14\u52a8\u6001\u89c4\u5212\u8fd9\u79cd\u901a\u8fc7\u7a7a\u95f4\u6362\u53d6\u65f6\u95f4\u7684\u7b97\u6cd5\u601d\u60f3\u5728\u5b9e\u9645\u7684\u5de5\u4f5c\u4e2d\u4e5f\u4f1a\u88ab\u9891\u7e41\u7528\u5230\u3002\u52a8\u6001\u89c4\u5212\u5bf9\u601d\u7ef4\u662f\u4e00\u4e2a\u5f88\u5927\u7684\u6311\u6218\u3002<br \/>\n\u52a8\u6001\u89c4\u5212\u7684\u57fa\u672c\u601d\u60f3\u5c31\u662f\u628a\u4e00\u4e2a\u5927\u95ee\u9898\u5206\u89e3\u6210\u89c4\u6a21\u6bd4\u5b83\u5c0f\u7684\u95ee\u9898\uff0c\u7136\u540e\u518d\u53bb\u89e3\u51b3\u5927\u95ee\u9898\uff0c\u4ec5\u6b64\u800c\u5df2\u3002\u6bd4\u5982\u6211\u4eec\u8981\u8ba1\u7b97\u524dn\u4e2a\u6570\u7684\u548cf(n)\uff0c\u90a3\u6211\u4eec\u53ef\u4ee5\u5148\u8ba1\u7b97\u51fa\u524dn-1\u4e2a\u6570\u7684\u548cf(n-1)\uff0c\u7136\u540e\u518d\u8003\u8651f(n)\u548cf(n-1)\u7684\u5173\u7cfb:f(n) = f(n-1) + n\uff0c\u90a3\u4e48\u8fd9\u4e2a\u95ee\u9898\u5c31\u53ef\u4ee5\u4ece\u5c0f\u95ee\u9898\u5f00\u59cb\u4e00\u6b65\u4e00\u6b65\u89e3\u51b3\uff0c\u76f4\u81f3\u89e3\u51b3\u6700\u7ec8\u7684\u95ee\u9898\u3002\u8fd9\u4e2a\u5c0f\u95ee\u9898\u7684\u7ec8\u70b9\u5c31\u662f\u6211\u4eec\u6240\u8bf4\u7684\u521d\u59cb\u72b6\u6001\uff0c\u7136\u540e\u8003\u8651f(n)\u548cf(n-1)\u7684\u5173\u7cfb\uff0c\u7c7b\u4f3c\u4e8e\u6570\u5b66\u5f52\u7eb3\u6cd5\u7684\u6837\u5b50\u3002<br \/>\n\u6240\u4ee5\uff0c\u52a8\u6001\u89c4\u5212\u4e00\u822c\u5206\u4e3a4\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<blockquote>\n<p><strong>\u95ee\u9898\u62c6\u89e3<\/strong><br \/>\n<strong>\u72b6\u6001\u5b9a\u4e49<\/strong><br \/>\n<strong>\u65b9\u7a0b\u5b9a\u4e49<\/strong><br \/>\n<strong>\u5b9e\u73b0<\/strong><\/p>\n<\/blockquote>\n<p>\u8fd8\u662f\u653e\u5230\u4ee5\u540e\u5199\u5427\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002\u3002<\/p>\n<p>\u4e0b\u9762\u4ecb\u7ecd\u51e0\u79cd\u5e38\u89c1\u7684\uff0c\u7b80\u5355\u52a8\u6001\u89c4\u5212\u3002<br \/>\n\u5728\u6240\u6709\u52a8\u6001\u89c4\u5212\u4e2d\uff0c\u6700\u7b80\u5355\u7684\u5e94\u8be5\u5c31\u662f\u7ebf\u6027\u52a8\u89c4\u4e86\u3002<\/p>\n<h4>\u722c\u697c\u68af   <a href=\"http:\/\/acm.hdu.edu.cn\/showproblem.php?pid=2041\">HDU 2041<\/a><\/h4>\n<p>\u5047\u8bbe\u4f60\u6b63\u5728\u722c\u697c\u68af\uff0c\u4ece\u7b2c0\u5c42\u5f00\u59cb\uff0c\u5230\u7b2cn\u5c42\uff0c\u6bcf\u6b21\u4f60\u53ef\u4ee5\u722c1\u9636\u6216\u80052\u9636\uff0c\u95ee\u4ece\u7b2c0\u5230\u7b2cn\u9636\u697c\u68af\u5171\u6709\u591a\u5c11\u79cd\u4e0d\u540c\u7684\u722c\u6cd5\u3002<br \/>\n\u53ef\u4ee5\u5f88\u7b80\u5355\u60f3\u5230\uff1af(1)=1,f(2)=1,f(n)=f(n)+f(n-1).<br \/>\n\u6700\u540e\u90a3\u4e2a\u5f0f\u5b50\u5c31\u662f\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e86\u3002<\/p>\n<pre><code class=\"language-c++\">cin &gt;&gt; n;\nf[1] = f[2] = 1;\nfor (int i=3;i&lt;=n;i++)   f[n] = f[n-1] + f[n-2];<\/code><\/pre>\n<p>\u8fd9\u5176\u5b9e\u548cFibonacci\u6570\u5217\u5dee\u4e0d\u591a\uff0c\u5f53n\u975e\u5e38\u5de8\u5927\u65f6\uff0c\u6734\u7d20\u7684\u4f5c\u6cd5\u884c\u4e0d\u901a\uff0c\u5c31\u8981\u7528\u5230\u77e9\u9635\u5feb\u901f\u5e42\u4e86\u3002<\/p>\n<p>\u8fd9\u9898\u6709\u51e0\u4e2a\u53d8\u79cd<br \/>\n\u6bcf\u6b21\u8f93\u5165n\uff0ck\uff0c\u5373\u6bcf\u6b21\u6700\u591a\u53ef\u722ck\u9636\u697c\u68af\uff0c\u95ee\u722c\u5230\u7b2cn\u9636\u6709\u591a\u5c11\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u3002   \u5b9e\u5728\u8bb0\u4e0d\u8d77\u6765\u5728\u54ea\u505a\u7684\u4e86\u3002\u3002\u5148\u628af[1~k]\u521d\u59cb\u5316\u4e3a1\uff0c\u7136\u540e\u518d\u7528\u6734\u7d20\u4f5c\u6cd5\u3002<\/p>\n<h4>\u4e09\u89d2\u5f62\u6700\u5c0f\u8def\u5f84\u4e4b\u548c   <a href=\"http:\/\/poj.org\/problem?id=1163\">POJ 1163<\/a><\/h4>\n<p>\u8f93\u5165\u4e00\u4e2a\u4e09\u89d2\u5f62<br \/>\n[<br \/>\n\u00b7\u00b7\u00b7\u00b7\u00b7[2],<br \/>\n\u00b7\u00b7\u00b7\u00b7[3,4],<br \/>\n\u00b7\u00b7\u00b7[6,5,7],<br \/>\n\u00b7\u00b7[4,1,8,3]<br \/>\n]<br \/>\n\u627e\u51fa\u81ea\u9876\u5411\u4e0b\u7684\u8def\u5f84\u6700\u77ed\u7684\u4e00\u6761\u3002<br \/>\ndp[i][j] = min(dp[i+1][j],dp[i+1][j+1])+a[i][j];<\/p>\n<pre><code class=\"language-c++\">#include &lt;cstdio&gt;\n#include &lt;algorithm&gt;\nusing namespace std;\nint a[105][105],b[105][105];\nint main(){\n    int t,n;\n    scanf (&quot;%d&quot;,&amp;n);\n    for (int i=1;i&lt;=n;i++)  for (int j=1;j&lt;=i;j++)  scanf (&quot;%d&quot;,&amp;a[i][j]);\n    for (int i=n;i&gt;=1;i--)  for (int j=1;j&lt;=i;j++)\n        b[i][j] = max(b[i+1][j],b[i+1][j+1])+a[i][j];\n    printf (&quot;%d\\n&quot;,b[1][1]);\n    return 0;\n}<\/code><\/pre>\n<h3>\u6700\u5927\u5b50\u5e8f\u548c   <a href=\"https:\/\/www.luogu.com.cn\/problem\/P1115\">\u6d1b\u8c37P1115<\/a><\/h3>\n<p>\u8fd9\u4e2a\u5c31\u6bd4\u8f83\u7ecf\u5178\u4e86\uff01<br \/>\n\u8f93\u5165\u4e00\u5217\u6570\uff0c\u6c42\u8fd9\u4e2a\u6570\u5217\u7684\u8fde\u7eed\u975e\u7a7a\u7684\u4e00\u6bb5\uff0c\u662f\u7684\u5176\u5b50\u6bb5\u548c\u6700\u5927\u3002<br \/>\n\u8fd9\u4e2a\u9898\u53ef\u4ee5\u7528\u7ebf\u6027\u52a8\u6001\u89c4\u5212\u89e3\uff0c\u4e5f\u53ef\u4ee5\u7528\u5206\u6cbb\u6cd5\u6c42\u89e3\uff0c\u90fd\u5f88\u5de7\u5999\u3002<br \/>\n\u6b64\u9898\u5982\u679c\u4e0d\u5e94\u7528\u4e00\u4e9b\u7b97\u6cd5\u7684\u8bdd\uff0c\u4e4d\u4e00\u770b\u662f\u5f88\u832b\u7136\u7684\uff0c\u603b\u4e0d\u80fd\u628a\u6240\u6709\u5b50\u6bb5\u90fd\u8bd5\u4e00\u904d\uff1f\uff1f\u90a3\u80af\u5b9a\u4e0d\u79d1\u5b66\uff0c<code class=\"katex-inline\">2^n<\/code>\u79cd\u60c5\u51b5\u3002<br \/>\n\u5148\u6765\u770b\u5206\u6cbb\u6cd5\u6c42\u89e3\u3002\u5728\u8fd9\u4e2a\u957f\u4e3an\u7684\u5e8f\u5217\u4e2d\uff0c\u6211\u4eec\u628a\u5b83\u5206\u6210\u5de6\u53f3\u5747\u7b49\u7684\u4e24\u90e8\u5206\uff0c\u548c\u6700\u5927\u7684\u90a3\u4e2a\u5b50\u6bb5\uff0c\u8981\u4e48\u5b8c\u5168\u5728\u5de6\u534a\u90e8\u5206\uff0c\u8981\u4e48\u5b8c\u5168\u5728\u53f3\u534a\u90e8\u5206\uff0c\u8981\u4e48\u4e24\u8fb9\u90fd\u6709\uff0c\u90a3\u4e48\u8fd9\u6837\u5c31\u53ef\u4ee5\u5206\u89e3\u89c4\u6a21\u4e86\u3002<br \/>\nf[1-n] = max{f[1-mid],f[mid+1-n],tmp};<br \/>\n\u5176\u4e2df[1-n]\u8868\u793a\u4e0b\u6807\u4e3a1-n\u5185\u7684\u6700\u4f18\u89e3\uff0ctmp\u8868\u793a\u4ece\u5e8f\u5217\u4e2d\u5fc3\u5f00\u59cb\uff0c\u5206\u522b\u5411\u5de6\u53f3\u4e24\u7aef\u6269\u5c55\u5f97\u5230\u7684\u6700\u5927\u548c\u7684\u548c\u3002tmp\u4ee3\u8868\u7684\u662f\u5de6\u53f3\u4e24\u8fb9\u90fd\u6709\u7684\u60c5\u51b5\u4e0b\u7684\u6700\u4f18\u89e3\u3002<\/p>\n<pre><code class=\"language-c++\">#include &lt;bits\/stdc++.h&gt;\nusing namespace std;\ntypedef long long ll;\n#define maxn 200005\nll a[maxn];\nll search(int l,int r);\nint main(){\n    int n;\n    scanf(&quot;%d&quot;,&amp;n);\n    for (int i=1;i&lt;=n;i++)  scanf(&quot;%lld&quot;,&amp;a[i]);\n    cout &lt;&lt; search(1,n);\n    return 0;\n}\nll search(int l,int r){\n    if (l==r)   return a[l];\n    if (r==l+1) return (ll)max(max(a[l],a[r]),a[l]+a[r]);\n    ll mid = (ll)max(search(l,(l+r)\/2),search((l+r)\/2+1,r)),now1,now2=0,ans1=-9999999,ans2=-99999999;\n    int temp = (l+r)\/2;\n    ans1=now1=a[temp];\n    for (int i=temp-1;i&gt;=l;i--){\n        now1 += a[i];\n        ans1 = max(ans1,now1);\n    }\n    for (int i=temp+1;i&lt;=r;i++){\n        now2 += a[i];\n        ans2 = max(ans2,now2);\n    }\n    return (ll)max((ll)ans1+ans2,mid);\n}<\/code><\/pre>\n<p>\u52a8\u6001\u89c4\u5212\u6c42\u89e3\u3002\u5148\u5047\u8bbe\u5728\u524di\u4e2a\u5e8f\u5217\u4e2d\uff0c\u6700\u4f18\u89e3\u7684\u6700\u540e\u4e00\u4e2a\u6570\uff0c\u8981\u4e48\u662f\u7b2ci\u4e2a\u6570\uff0c\u8981\u4e48\u4e0d\u662f\uff0c\u5373\u524di\u4e2a\u6570\u7684\u6700\u4f18\u89e3\u8981\u4e48\u5305\u542b\u7b2ci\u4e2a\u6570\uff0c\u8981\u4e48\u4e0d\u5305\u542b\u3002\u8bbe\u51fa\u4e24\u4e2a\u53d8\u91cfdp[i]\uff0ctmp\uff0cdp[i]\u8868\u793a\u524di\u4e2a\u6570\u5f97\u6700\u4f18\u89e3\uff0ctmp\u8868\u793a\u5728\u524d\u4e2a\u6570\u4e2d\uff0c\u4ee5a[i]\u7ed3\u5c3e\u7684\u6700\u5927\u5b50\u6bb5\u548c\u3002<br \/>\n\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff1adp[i+1] = max(dp[i],max(a[i+1],tmp+a[i+1]))<br \/>\ntmp\u4e5f\u8981\u66f4\u65b0\uff1atmp = max(a[i+1],a[i+1]+tmp);<\/p>\n<pre><code class=\"language-c++\">\n#include &lt;bits\/stdc++.h&gt;\nusing namespace std;\n#define maxn 200005\nint a[maxn];\nvoid read(int &amp;x){        \/\/\u8fd9\u91ccx\u662f\u6574\u6570\uff0cint,ll\u90fd\u53ef\n    int f=1;x=0;char s = getchar();  \/\/f\u8868\u793a\u7b26\u53f7\n    while(s&lt;&#039;0&#039; || s&gt;&#039;9&#039;){if (s==&#039;-&#039;)f = -1;s = getchar();}\n    while(s&gt;=&#039;0&#039; &amp;&amp; s&lt;=&#039;9&#039;){x = x*10+s-&#039;0&#039;;s=getchar();}\n    x *= f;\n}\nint main(){\n    int n;\n    read(n);\n    for (int i=1;i&lt;=n;i++)  read(a[i]);\n    int tmp,ans;\n    tmp = ans = a[1];\n    for (int i=2;i&lt;=n;i++){\n        tmp = max(a[i],tmp+a[i]);\n        ans = max(ans,tmp);\n    }\n    printf (&quot;%d&quot;,ans);\n    return 0;\n}<\/code><\/pre>\n<p>\u5347\u7ea7\u7248\uff1a<a href=\"http:\/\/poj.org\/problem?id=2479\">POJ2479<\/a><br \/>\n<img src=\"http:\/\/47.103.223.234:100\/wp-content\/uploads\/2020\/04\/formula.png\" alt=\"\" \/><br \/>\nA\u4e3a\u4e00\u4e2a\u5e8f\u5217\uff0c\u6c42d[A].<br \/>\n\u6b63\u53cd\u6c42\u4e24\u904ddp\uff0cdp1[i]\u8868\u793a\u524di\u4e2a\u6570\u7684\u6700\u4f18\u89e3\uff0cdp2[i]\u8868\u793ai-n\u7684\u6700\u4f18\u89e3\u3002\u7136\u540e\u5728O(n)\u8dd1\u4e00\u904d\u539f\u6570\u7ec4\uff0c\u53bb\u6700\u5927\u503c\u5373\u53ef\u3002<br \/>\nd(A) = max(dp1[i]+dp2[i+1]);<\/p>\n<pre><code class=\"language-c++\">#include &lt;cstdio&gt;\n#include &lt;algorithm&gt;\n#include &lt;cstring&gt;\nusing namespace std;\n#define wa -100000000\nconst int maxn = 5e4+5;\nint a[maxn];\nint dp1[maxn],dp2[maxn];\nint main(){\n    int t,n;\n    scanf (&quot;%d&quot;,&amp;t);\n    while (t--){\n        scanf (&quot;%d&quot;,&amp;n);\n        memset(dp1,0,sizeof dp1);\n        memset(dp2,0,sizeof dp2);\n        for (int i=1;i&lt;=n;i++)  scanf (&quot;%d&quot;,&amp;a[i]);\n        int tmp = a[1]; dp1[1] = a[1];\n        for (int i=2;i&lt;=n;i++){\n            tmp = max(a[i],a[i]+tmp);\n            dp1[i] = max(dp1[i-1],tmp);\n        }\n        tmp = a[n],dp2[n] = a[n];\n        for (int i=n-1;i&gt;=1;i--){\n            tmp = max(a[i],a[i]+tmp);\n            dp2[i] = max(dp2[i+1],tmp);\n        }\n        int ans = wa;\n        \/\/int ans = dp1[1]+dp2[2];\n        for (int i=1;i&lt;n;i++)\n            ans = max(ans,dp1[i]+dp2[i+1]);\n        printf (&quot;%d\\n&quot;,ans);\n    }\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u52a8\u6001\u89c4\u5212(Dynamic Programming) \u52a8\u6001\u89c4\u5212\u95ee\u9898\u4e00\u76f4\u662f\u7b97\u6cd5\u9762\u8bd5\u5f53\u4e2d\u7684\u91cd\u70b9\u548c\u96be\u70b9\uff0c\u5e76\u4e14 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22],"tags":[],"_links":{"self":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/226"}],"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=226"}],"version-history":[{"count":12,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/226\/revisions"}],"predecessor-version":[{"id":239,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/226\/revisions\/239"}],"wp:attachment":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/media?parent=226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/categories?post=226"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/tags?post=226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}