{"id":2326,"date":"2023-02-14T22:25:39","date_gmt":"2023-02-14T14:25:39","guid":{"rendered":"https:\/\/www.shuizilong.com\/house\/?p=2326"},"modified":"2023-02-15T01:51:27","modified_gmt":"2023-02-14T17:51:27","slug":"dp-%e4%bc%98%e5%8c%96%e7%bb%83%e4%b9%a0","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/dp-%e4%bc%98%e5%8c%96%e7%bb%83%e4%b9%a0\/","title":{"rendered":"DP \u4f18\u5316\u7ec3\u4e60"},"content":{"rendered":"<ul>\n<li><a href=\"https:\/\/www.cnblogs.com\/flashhu\/p\/9480669.html\">https:\/\/www.cnblogs.com\/flashhu\/p\/9480669.html<\/a><\/li>\n<\/ul>\n<h2>Luogu P2513. [HAOI2009]\u9006\u5e8f\u5bf9\u6570\u5217<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.luogu.com.cn\/problem\/P2513\">https:\/\/www.luogu.com.cn\/problem\/P2513<\/a><\/li>\n<\/ul>\n<pre class=\"brush: cpp; light: false; title: \u66b4\u529b\uff08\u4ec0\u4e48\uff0c\u5c45\u7136\u80fd\u8fc7\uff1f...\uff09; toolbar: true; notranslate\" title=\"\u66b4\u529b\uff08\u4ec0\u4e48\uff0c\u5c45\u7136\u80fd\u8fc7\uff1f...\uff09\">\r\n#include &lt;lastweapon\/io&gt;\r\nusing namespace lastweapon;\r\n\r\nconst int N = int(1e3) + 9;\r\nint f&#x5B;N];\r\nint n, m;\r\n\r\nint main(){\r\n\r\n#ifndef ONLINE_JUDGE\r\n     \/\/freopen(&quot;in.txt&quot;, &quot;r&quot;, stdin);\r\n#endif\r\n\r\n    RD(n, m); f&#x5B;0] = 1;\r\n    FOR(i, 1, n) {\r\n        DWN_1(j, m, 1) {\r\n            int t = max(0, j-i);\r\n            DWN(k, j, t) f&#x5B;j] += f&#x5B;k];\r\n            f&#x5B;j] %= 10000;\r\n        }\r\n    }\r\n    cout &lt;&lt; f&#x5B;m] &lt;&lt; endl;\r\n}\r\n<\/pre>\n<pre class=\"brush: cpp; light: false; title: \u90e8\u5206\u548c; toolbar: true; notranslate\" title=\"\u90e8\u5206\u548c\">\r\n#include &lt;lastweapon\/io&gt;\r\nusing namespace lastweapon;\r\n\r\nconst int N = int(1e3) + 9;\r\nint f&#x5B;N], s&#x5B;N];\r\nint n, m;\r\n\r\nint main(){\r\n\r\n#ifndef ONLINE_JUDGE\r\n     \/\/freopen(&quot;in.txt&quot;, &quot;r&quot;, stdin);\r\n#endif\r\n\r\n    RD(n, m); f&#x5B;0] = 1;\r\n    FOR(i, 1, n) {\r\n        REP(j, m) s&#x5B;j+1] = s&#x5B;j] + f&#x5B;j];\r\n        DWN_1(j, m, 1) {\r\n            f&#x5B;j] += s&#x5B;j] - s&#x5B;max(0, j-i)];\r\n            f&#x5B;j] %= 10000;\r\n        }\r\n    }\r\n    cout &lt;&lt; f&#x5B;m] &lt;&lt; endl;\r\n}\r\n\r\n<\/pre>\n<h2>Luogu P2511. [HAOI2008]\u6728\u68cd\u5206\u5272<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.luogu.com.cn\/problem\/P2511\">https:\/\/www.luogu.com.cn\/problem\/P2511<\/a><\/li>\n<\/ul>\n<p>\u8fd9\u4e2a\u66b4\u529b\u53cd\u800c\u4e0d\u592a\u597d\u5199\u3002\u3002\u3002\u5bf9\u4e8e\u6bcf\u4e2a\u72b6\u6001\uff0c\u5927\u6982\u9700\u8981\u5206\u522b\u6709 best[i][j], ways[i][j] \u8868\u793a\u957f\u5ea6\u4e3a j \u6070\u597d\u5207 i \u6bb5\uff0c\u65f6\u7684\u6700\u4f18\u89e3\u548c\u65b9\u6848\u6570\u3002<br \/>\n\u4f46\u8f6c\u79fb\u7684\u65b9\u7a0b\u91cc\u9762\u6709 max() \u5bfc\u81f4\u975e\u5e38\u96be\u4ee5\u4f18\u5316\u3002<\/p>\n<p>\u8fdb\u4e00\u6b65\u6211\u4eec\u53d1\u73b0 best \u662f\u53ef\u4ee5\u76f4\u63a5\u4e8c\u5206\u51fa\u6765\u7684\u3002\u3002\u3002\u6709\u4e86\u6700\u5927\u5207\u75d5\u957f\u5ea6\uff0c\u5728\u8003\u8651 dp \u65b9\u6848\u6570\u5c31\u7b80\u5316\u975e\u5e38\u591a\u4e86\u3002\u3002\uff08\u6b64\u65f6\u6211\u4eec\u5728\u6700\u540e\u7684\u6700\u5927\u5207\u75d5\u4e00\u5b9a\u7b49\u4e8e best\u3002\u3002\u5373\u4f7f\u4e2d\u95f4\u5141\u8bb8\u4e0d\u6ee1\u8db3\uff09<\/p>\n<pre class=\"brush: cpp; light: false; title: \u90e8\u5206\u548c; toolbar: true; notranslate\" title=\"\u90e8\u5206\u548c\">\r\n#include &lt;lastweapon\/io&gt;\r\nusing namespace lastweapon;\r\n\r\nconst int N = int(5e4) + 9;\r\nint a&#x5B;N], s&#x5B;N], lt&#x5B;N]; int ways&#x5B;N], sums&#x5B;N];\r\nint n, m, p, q;\r\n\r\nbool ok(int x) {\r\n    int cur = 0, cnt = 0;\r\n    REP_1(i, n) {\r\n        if (cur + a&#x5B;i] &gt; x) cur = 0, ++cnt;\r\n        cur += a&#x5B;i];\r\n    }\r\n    return cnt &lt;= m;\r\n}\r\n\r\nint get_cut() {\r\n    int l = *max_element(a+1, a+n+1), r = s&#x5B;n];\r\n    while (l &lt; r) {\r\n        int m = (l + r) \/ 2;\r\n        if (ok(m)) r = m;\r\n        else l = m + 1;\r\n    }\r\n    return l;\r\n}\r\n\r\nint main() {\r\n\r\n#ifndef ONLINE_JUDGE\r\n     freopen(&quot;in.txt&quot;, &quot;r&quot;, stdin);\r\n#endif\r\n\r\n    MOD = 10007;\r\n    RD(n, m); REP_1(i, n) s&#x5B;i] = s&#x5B;i-1] + RD(a&#x5B;i]);\r\n    int cut = get_cut();\r\n\r\n    int l = 0; REP_1(i, n) {\r\n        while (s&#x5B;i] - s&#x5B;l] &gt; cut) ++l;\r\n        lt&#x5B;i] = l;\r\n    }\r\n\r\n    REP_1(i, n) if (s&#x5B;i] &lt;= cut) ways&#x5B;i] = 1; else break;\r\n    int tot = ways&#x5B;n];\r\n\r\n    REP_1(i, m) {\r\n        REP(j, n) sums&#x5B;j+1] = sums&#x5B;j] + ways&#x5B;j];\r\n        REP_1(j, n) {\r\n            ways&#x5B;j] = (sums&#x5B;j] - sums&#x5B;lt&#x5B;j]]) % MOD;\r\n            if (ways&#x5B;j] &lt; 0) ways&#x5B;j] += MOD;\r\n        }\r\n        tot += ways&#x5B;n];\r\n    }\r\n    tot %= MOD;\r\n    printf(&quot;%d %d\\n&quot;, cut, int(tot));\r\n}\r\n\r\n<\/pre>\n<h2>P3515 [POI2011]Lightning Conductor<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.luogu.com.cn\/problem\/P3515\">https:\/\/www.luogu.com.cn\/problem\/P3515<\/a><\/li>\n<\/ul>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\n#include &lt;lastweapon\/io&gt;\r\nusing namespace lastweapon;\r\n\r\nconst int N = int(5e5) + 9;\r\nint a&#x5B;N];\r\nint n;\r\n\r\n\r\nint main() {\r\n\r\n#ifndef ONLINE_JUDGE\r\n     freopen(&quot;in.txt&quot;, &quot;r&quot;, stdin);\r\n#endif\r\n\r\n    RD(n); REP(i, n) RD(a&#x5B;i]);\r\n\r\n    REP(i, n) {\r\n        int z = 0;\r\n\r\n        REP(j, n) {\r\n            int t = sqrt(abs(i-j)); if (t*t &lt; abs(i-j)) ++t;\r\n            checkMax(z, (a&#x5B;j] - a&#x5B;i]) + t);\r\n        }\r\n        printf(&quot;%d\\n&quot;, z);\r\n    }\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/www.cnblogs.com\/flashhu\/p\/9480669.html Luogu P2513. [HAOI2009]\u9006\u5e8f\u5bf9\u6570\u5217 https:\/\/www.luogu.com.cn\/problem\/P2513 #include &lt;lastweapon\/io&gt; using namespace lastweapon; const int N = int(1e3) + 9; int f&#x5B;N]; int n, m; int main(){ #ifndef ONLINE_JUDGE \/\/freopen(&quot;in.txt&quot;, &quot;r&quot;, stdin); #endif RD(n, m); f&#x5B;0] = 1; FOR(i, 1, n) { DWN_1(j, m, 1) { int t = max(0, j-i); DWN(k, j, t) f&#x5B;j] += f&#x5B;k]; f&#x5B;j] [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[1],"tags":[],"class_list":["post-2326","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-Bw","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/2326","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/comments?post=2326"}],"version-history":[{"count":9,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/2326\/revisions"}],"predecessor-version":[{"id":2336,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/2326\/revisions\/2336"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=2326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=2326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=2326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}