{"id":47,"date":"2026-02-22T19:33:56","date_gmt":"2026-02-22T11:33:56","guid":{"rendered":"https:\/\/blog.mirpri.com\/?p=47"},"modified":"2026-02-22T19:36:31","modified_gmt":"2026-02-22T11:36:31","slug":"luogu-p2803","status":"publish","type":"post","link":"https:\/\/blog.mirpri.com\/index.php\/luogu-p2803\/","title":{"rendered":"P2803 \u5b66\u6821\u9009\u5740 II"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u9898\u76ee\u63cf\u8ff0<\/h2>\n\n\n\n<p>\u5728\u4e00\u6761\u5927\u8def\u4e00\u65c1\u6709\u8bb8\u591a\u680b\u697c\uff0c\u6bcf\u680b\u697c\u91cc\u6709\u8bb8\u591a\u5c0f\u5b66\u751f\u3002\u6240\u4ee5\u552f\u6050\u4e16\u754c\u4e0d\u4e71\u7684\u725bA\u6253\u7b97\u5728\u8def\u4e0a\u4efb\u9009\u51e0\u70b9\uff08\u53ef\u4ee5\u548c\u697c\u91cd\u5408\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u4e0d\u91cd\u5408\uff09\u5efa\u7acb\u5c0f\u5b66\uff0c\u4e14\u4f7f\u6240\u6709\u5c0f\u5b66\u751f\u4e0a\u5b66\u8d70\u7684\u8def\u7a0b\u4e4b\u548c\u6700\u77ed\u3002\u725bA\u53d1\u73b0\u4fee\u5efa\u4e00\u6240\u5c0f\u5b66\u6839\u672c\u65e0\u6cd5\u6ee1\u8db3\u4ed6\u552f\u6050\u4e16\u754c\u4e0d\u4e71\u7684\u5fc3\u7406\uff0c\u6240\u4ee5\u4ed6\u51c6\u5907\u5efa\u7acbK\u6240\u5c0f\u5b66\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8f93\u5165\u683c\u5f0f<\/h3>\n\n\n\n<p>\u7b2c\u4e00\u884c2\u4e2a\u6574\u6570\uff0c\u8868\u793a\u697c\u6570n\uff0c\u5b66\u6821\u6570k\uff081&lt;=n,k&lt;=100\uff09<\/p>\n\n\n\n<p>\u7b2c\u4e8c\u884cn\u4e2a\u6574\u6570\uff0c\u8868\u793a\u6bcf\u680b\u697c\u7684\u5b66\u751f\u6570\uff080\uff1c\u6bcf\u680b\u697c\u5b66\u751f\u6570\u2264100 )<\/p>\n\n\n\n<p>\u7b2c\u4e09\u884cn-1\u4e2a\u6570\uff0c\u5206\u522b\u8868\u793a\u697ci\u5230\u697ci+1\u4e4b\u95f4\u8ddd\u79bb\uff081\u2264\u8ddd\u79bb\u2264100\uff0c1\u2264i\u2264n-1\uff09<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8f93\u51fa\u683c\u5f0f<\/h3>\n\n\n\n<p>\u5373\u5b66\u751f\u8d70\u7684\u8ddd\u79bb\u548c\u7684\u6700\u5c0f\u503c<\/p>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button is-style-outline is-style-outline--1\"><a class=\"wp-block-button__link wp-element-button\">Luogu P2803<\/a><\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u7b54<\/h2>\n\n\n\n<p>\u4f7f\u7528\u52a8\u6001\u89c4\u5212\u3002\u201c\u9009 k \u4e2a\u70b9\u6c42\u6700\u5c0f\u4ee3\u4ef7\u201d\u2192 \u60f3\u5230\u627e\u6700\u4f18\u5b50\u7ed3\u6784\u2192DP<\/p>\n\n\n\n<p><strong>\u5b9a\u4e49DP\u72b6\u6001<\/strong>\uff1a<code>f[i][j]<\/code>\u8868\u793a \u201c\u524d i \u680b\u697c\u5efa j \u6240\u5c0f\u5b66\u7684\u6700\u5c0f\u603b\u8def\u7a0b\u201d<\/p>\n\n\n\n<p>\u5219\u7b54\u6848\u5373\u4e3a<code>f[n-1][k]<\/code> (\u4ece\u7b2c0\u680b\u5f00\u59cb)<\/p>\n\n\n\n<p><strong>\u72b6\u6001\u8f6c\u79fb<\/strong>\uff1a\u8003\u8651\u533a\u95f4[0,i]\u5185\u6700\u540e\u4e00\u6240\u5b66\u6821\u8986\u76d6\u7684\u8303\u56f4\uff0c\u5f53\u8986\u76d6\u7684\u8303\u56f4\u4e3a[t+1,i]\u65f6\uff0c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>f&#91;i]&#91;j]=f&#91;t]&#91;j-1] \/\/ \u524dt\u680b\u697c\u6709j-1\u4e2a\u5b66\u6821\n\n        +cost&#91;t+1]&#91;i] \/\/ t+1\u81f3i\u67091\u4e2a\u5b66\u6821<\/code><\/pre>\n\n\n\n<p><code>cost[i][j]<\/code>\u8868\u793a[i,j]\u533a\u95f4\u6709\u4e00\u6240\u5b66\u6821\u65f6\u7684\u6700\u5c0f\u603b\u8def\u7a0b\u3002(\u5b50\u95ee\u9898\uff0c\u5173\u952e\uff01)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u9884\u5904\u7406<code>cost<\/code>\u6570\u7ec4<\/h3>\n\n\n\n<p>\u533a\u95f4\u786e\u5b9a\u65f6\uff0c\u663e\u7136\u5b66\u6821\u5e94\u5efa\u5728\u4e00\u5904\u4f7f\u5f97\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5de6\u4fa7+\u8fd9\u680b\u697c\u7684\u5b66\u751f\u6570 &gt;= \u53f3\u4fa7\u5b66\u751f\u6570\uff08\u5426\u5219\u5b66\u6821\u79fb\u5230\u53f3\u8fb9\u90a3\u680b\u697c\u53ef\u4f7f\u603b\u8ddd\u79bb\u51cf\u5c11\uff09 <strong>\u4e14<\/strong> \u53f3\u4fa7+\u8fd9\u680b\u697c\u7684\u5b66\u751f\u6570 &gt;= \u5de6\u4fa7\u5b66\u751f\u6570\uff08\u540c\u7406\uff09<\/li>\n\n\n\n<li>\u5373 <strong>\u7b2c\u4e00\u4e2a<\/strong>\u6ee1\u8db3 \u5de6\u4fa7+\u8fd9\u680b\u697c\u7684\u5b66\u751f\u6570 &gt;= \u53f3\u4fa7\u5b66\u751f\u6570 \u7684\u4f4d\u7f6e<\/li>\n\n\n\n<li>\u5373 <strong>\u7b2c\u4e00\u4e2a<\/strong> \u5de6\u4fa7+\u8fd9\u680b\u697c\u7684\u5b66\u751f\u6570 &gt;= <strong>\u533a\u95f4\u5b66\u751f\u6570\u7684\u4e00\u534a<\/strong> \u7684\u4f4d\u7f6e\uff08\u53ef\u7528<code>lower_bound<\/code>\uff09<\/li>\n<\/ul>\n\n\n\n<p>\u636e\u6b64\u8ba1\u7b97<code>cost<\/code>\u6570\u7ec4\u6bcf\u4e00\u9879\uff0c\u590d\u6742\u5ea6\u90fd\u4e3a<math data-latex=\"O(n)\"><semantics><mrow><mi>O<\/mi><mo form=\"prefix\" stretchy=\"false\">(<\/mo><mi>n<\/mi><mo form=\"postfix\" stretchy=\"false\">)<\/mo><\/mrow><annotation encoding=\"application\/x-tex\">O(n)<\/annotation><\/semantics><\/math><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">AC\u4ee3\u7801<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;algorithm&gt;\nusing namespace std;\n\nconst int mm = 110;\nint n, k, d&#91;mm], s&#91;mm];\nint f&#91;mm]&#91;mm], cost&#91;mm]&#91;mm];\n\nint main(){\n    cin&gt;&gt;n&gt;&gt;k;\n\n    for (int i=1; i&lt;=n; i++) {\n        int tmp;\n        cin&gt;&gt;tmp;\n        s&#91;i]=s&#91;i-1]+tmp;\n    }\n\n    for (int i=1; i&lt;n; i++) {\n        int tmp;\n        cin&gt;&gt;tmp;\n        d&#91;i]=d&#91;i-1]+tmp;\n    }\n\n    for (int i=0; i&lt;n; i++) {\n        for (int j=0; j&lt;n; j++) {\n            if(i==j){\n                cost&#91;i]&#91;j]=0;\n                continue;\n            }\n            int school=lower_bound(s+i+1, s+j+1, (s&#91;j+1]+s&#91;i])\/2)-s-1;\n            for (int l=i; l&lt;=j; l++) {\n                cost&#91;i]&#91;j]+=(s&#91;l+1]-s&#91;l])*abs(d&#91;l]-d&#91;school]);\n            }\n        }\n    }\n\n    for (int i=0; i&lt;n; i++) {\n        for (int j=0; j&lt;=k; j++) {\n            f&#91;i]&#91;j]=1e8;\n        }\n    }\n\n    for (int i=0; i&lt;n; i++) {\n        f&#91;i]&#91;1]=cost&#91;0]&#91;i];\n    }\n\n    for (int i=2; i&lt;=k; i++) {\n        for (int j=i-1; j&lt;n; j++) {\n            for (int l=i-2; l&lt;j; l++) {\n                f&#91;j]&#91;i]=min(f&#91;j]&#91;i], f&#91;l]&#91;i-1]+cost&#91;l+1]&#91;j]);\n            }\n        }\n    }\n\n    cout&lt;&lt;f&#91;n-1]&#91;k];\n   \n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u5728\u4e00\u6761\u5927\u8def\u4e00\u65c1\u6709\u8bb8\u591a\u680b\u697c\uff0c\u6bcf\u680b\u697c\u91cc\u6709\u8bb8\u591a\u5c0f\u5b66\u751f\u3002\u6240\u4ee5\u552f\u6050\u4e16\u754c\u4e0d\u4e71\u7684\u725bA\u6253\u7b97\u5728\u8def\u4e0a\u4efb\u9009\u51e0\u70b9\uff08\u53ef\u4ee5\u548c\u697c\u91cd\u5408\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u4e0d\u91cd\u5408\uff09\u5efa\u7acb\u5c0f\u5b66\uff0c\u4e14\u4f7f\u6240\u6709\u5c0f\u5b66\u751f\u4e0a\u5b66\u8d70\u7684\u8def\u7a0b\u4e4b\u548c\u6700\u77ed\u3002\u725bA\u53d1\u73b0\u4fee\u5efa\u4e00\u6240\u5c0f\u5b66\u6839\u672c\u65e0\u6cd5\u6ee1\u8db3\u4ed6\u552f\u6050\u4e16\u754c\u4e0d\u4e71\u7684\u5fc3\u7406\uff0c\u6240\u4ee5\u4ed6\u51c6\u5907\u5efa\u7acbK\u6240\u5c0f\u5b66\u3002 \u8f93\u5165\u683c\u5f0f \u7b2c\u4e00\u884c2\u4e2a\u6574\u6570\uff0c\u8868\u793a\u697c\u6570n\uff0c\u5b66\u6821\u6570k\uff081&lt;=n,k&lt;=100\uff09 \u7b2c\u4e8c\u884cn\u4e2a\u6574\u6570\uff0c\u8868\u793a\u6bcf\u680b\u697c\u7684\u5b66\u751f\u6570\uff080\uff1c\u6bcf\u680b\u697c\u5b66\u751f\u6570\u2264100 ) \u7b2c\u4e09\u884cn-1\u4e2a\u6570\uff0c\u5206\u522b\u8868\u793a\u697ci\u5230\u697ci+1\u4e4b\u95f4\u8ddd\u79bb\uff081\u2264\u8ddd\u79bb\u2264100\uff0c1\u2264i\u2264n-1\uff09 \u8f93\u51fa\u683c\u5f0f \u5373\u5b66\u751f\u8d70\u7684\u8ddd\u79bb\u548c\u7684\u6700\u5c0f\u503c \u89e3\u7b54 \u4f7f\u7528\u52a8\u6001\u89c4\u5212\u3002\u201c\u9009 k \u4e2a\u70b9\u6c42\u6700\u5c0f\u4ee3\u4ef7\u201d\u2192 \u60f3\u5230\u627e\u6700\u4f18\u5b50\u7ed3\u6784\u2192DP \u5b9a\u4e49DP\u72b6\u6001\uff1af[i][j]\u8868\u793a \u201c\u524d i \u680b\u697c\u5efa j \u6240\u5c0f\u5b66\u7684\u6700\u5c0f\u603b\u8def\u7a0b\u201d \u5219\u7b54\u6848\u5373\u4e3af[n-1][k] (\u4ece\u7b2c0\u680b\u5f00\u59cb) \u72b6\u6001\u8f6c\u79fb\uff1a\u8003\u8651\u533a\u95f4[0,i]\u5185\u6700\u540e\u4e00\u6240\u5b66\u6821\u8986\u76d6\u7684\u8303\u56f4\uff0c\u5f53\u8986\u76d6\u7684\u8303\u56f4\u4e3a[t+1,i]\u65f6\uff0c cost[i][j]\u8868\u793a[i,j]\u533a\u95f4\u6709\u4e00\u6240\u5b66\u6821\u65f6\u7684\u6700\u5c0f\u603b\u8def\u7a0b\u3002(\u5b50\u95ee\u9898\uff0c\u5173\u952e\uff01) \u9884\u5904\u7406cost\u6570\u7ec4 \u533a\u95f4\u786e\u5b9a\u65f6\uff0c\u663e\u7136\u5b66\u6821\u5e94\u5efa\u5728\u4e00\u5904\u4f7f\u5f97\uff1a \u636e\u6b64\u8ba1\u7b97cost\u6570\u7ec4\u6bcf\u4e00\u9879\uff0c\u590d\u6742\u5ea6\u90fd\u4e3aO(n)O(n) AC\u4ee3\u7801<\/p>\n","protected":false},"author":4,"featured_media":43,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-47","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-algorithm"],"_links":{"self":[{"href":"https:\/\/blog.mirpri.com\/index.php\/wp-json\/wp\/v2\/posts\/47","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.mirpri.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mirpri.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mirpri.com\/index.php\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mirpri.com\/index.php\/wp-json\/wp\/v2\/comments?post=47"}],"version-history":[{"count":0,"href":"https:\/\/blog.mirpri.com\/index.php\/wp-json\/wp\/v2\/posts\/47\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.mirpri.com\/index.php\/wp-json\/wp\/v2\/media\/43"}],"wp:attachment":[{"href":"https:\/\/blog.mirpri.com\/index.php\/wp-json\/wp\/v2\/media?parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mirpri.com\/index.php\/wp-json\/wp\/v2\/categories?post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mirpri.com\/index.php\/wp-json\/wp\/v2\/tags?post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}