{"id":223,"date":"2023-05-17T21:17:18","date_gmt":"2023-05-17T13:17:18","guid":{"rendered":"https:\/\/solost.cn\/?p=223"},"modified":"2023-05-17T21:17:18","modified_gmt":"2023-05-17T13:17:18","slug":"336","status":"publish","type":"post","link":"https:\/\/solost.cn\/?p=223","title":{"rendered":"Educational Codeforces Round 109 (Rated for Div. 2) &#8211; D. Armchairs"},"content":{"rendered":"<h2>\u9898\u76ee\u94fe\u63a5<\/h2>\n<p><a href=\"https:\/\/codeforces.com\/problemset\/problem\/1525\/D\">\u94fe\u63a5<\/a><\/p>\n<h2>\u9898\u76ee\u5927\u610f<\/h2>\n<p>\u6709n\u4e2a\u5ea7\u4f4d\uff0c\u7ed9\u4e00\u4e2a\u6570\u5217\uff0c1\u4ee3\u8868\u6709\u4eba\uff0c0\u4ee3\u8868\u6ca1\u4eba\uff0c\u8ba9\u6240\u6709\u4eba\u5750\u5230\u5176\u4ed6\u6ca1\u4eba\u7684\u5730\u65b9\u53bb\uff0c\u79fb\u52a8\u7684\u82b1\u8d39\u662f\u4f4d\u7f6e\u7684\u5dee\u503c\uff0c\u95ee\u4f60\u6700\u5c11\u7684\u82b1\u8d39\u662f\u591a\u5c11\uff1f<\/p>\n<h2>\u9898\u89e3<\/h2>\n<p>\u8fd9\u9053\u9898\u7b2c\u4e00\u4e2a\u60f3\u6cd5\u662f\u628a\u7b2c\u4e00\u4e2a\u4eba\u5b89\u6392\u5230\u81ea\u5df1\u524d\u9762\u6240\u6709\u7a7a\u7684\u5730\u65b9\uff0c\u7136\u540e\u5c31\u6709\u5f88\u591a\u79cd\u5b89\u6392\u65b9\u6cd5\uff0c\u7136\u540e\u7b2c\u4e8c\u4e2a\u4eba\u518d\u5b89\u6392\u5230\u7b2c\u4e8c\u4e2a\u4eba\u524d\u9762\u7684\u5ea7\u4f4d\u4e0a\uff0c\u4e0d\u8981\u5b89\u6392\u5230\u94b1\u4e00\u4e2a\u4eba\u7684\u524d\u9762\u5c31\u884c\uff0c\u7136\u540e\u6765\u8ba1\u7b97\u6700\u5c0f\u82b1\u8d39\u3002\u597d\u50cf\u8fd9\u79cd\u5b9e\u73b0\u65b9\u6cd5\u6bd4\u8f83\u7e41\u7410\uff0c\u7136\u540e\u5c31\u91c7\u53d6\u4e0b\u9762\u7684\u65b9\u6cd5\u3002<\/p>\n<ul>\n<li>fi\u4ee3\u8868\u524di\u4e2a\u4eba\u524dj\u4e2a\u5ea7\u4f4d\u6709\u4eba\u5750\u7684\u6700\u5c0f\u82b1\u8d39\u3002<\/li>\n<li>\u5f53\u524d\u4eba\u5750\u5ea7\u4f4d<code>f[i][j] = f[i - 1][j - 1]<\/code><\/li>\n<li>\u5f53\u524d\u4eba\u4e0d\u5750\u5ea7\u4f4d<code>f[i][j] = f[i][j - 1]<\/code><\/li>\n<\/ul>\n<pre><code class=\"lang-cpp\">#include &lt;map&gt;\n#include &lt;set&gt;\n#include &lt;cmath&gt;\n#include &lt;queue&gt;\n#include &lt;stack&gt;\n#include &lt;vector&gt;\n#include &lt;bitset&gt;\n#include &lt;cstdio&gt;\n#include &lt;cstdlib&gt;\n#include &lt;cstring&gt;\n#include &lt;fstream&gt;\n#include &lt;iostream&gt;\n#include &lt;algorithm&gt;\n#include &lt;unordered_map&gt;\n#include &lt;unordered_set&gt;\n\n#define x first\n#define y second\n#define endl &#039;\\n&#039;\n#define IOS                       \\\n    ios_base::sync_with_stdio(0); \\\n    cin.tie(0);                   \\\n    cout.tie(0);\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair&lt;ll, ll&gt; pll;\ntypedef pair&lt;int, int&gt; pii;\ntypedef unsigned long long ull;\n\nconst int INF = 0x3f3f3f3f, mod = 1000000007;\nconst int N = 5010;\nint a[N], b[N];\nint f[N][N];\nvoid solve()\n{\n    int n;\n    cin &gt;&gt; n;\n    int cnt1 = 0, cnt2 = 0;\n    for(int i = 1; i &lt;= n; i++){\n        int x;\n        cin &gt;&gt; x;\n        if(x) a[++cnt1] = i;\n        else b[++cnt2] = i;\n    }\n    for(int i = 1; i &lt;= cnt1; i++){\n        for(int j = 1; j &lt;= cnt2; j++){\n            if(i == j){\n                f[i][j] = f[i - 1][j - 1] + abs(a[i] - b[j]);\/\/\u524di\u4e2a\u4eba\u4e0d\u53ef\u80fd\u5750\u5230\u524dj-1\u4e2a\u6905\u5b50\u4e0a\uff0c\u4eba\u6570\u4e0d\u80fd\u5c11\u4e8e\u6905\u5b50\u6570\u91cf\n            }\n            else{\n                f[i][j] = min(f[i][j - 1], f[i - 1][j - 1] + abs(a[i] - b[j]));\n            }\n        }\n    }\n    cout &lt;&lt; f[cnt1][cnt2] &lt;&lt; endl;\n}\nint main()\n{\n    \/\/ IOS;\n    int t = 1;\n    \/\/ cin &gt;&gt; t;\n    while (t--) {\n        solve();\n    }\n    return 0;\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u94fe\u63a5 \u94fe\u63a5 \u9898\u76ee\u5927\u610f \u6709n\u4e2a\u5ea7\u4f4d\uff0c\u7ed9\u4e00\u4e2a\u6570\u5217\uff0c1\u4ee3\u8868\u6709\u4eba\uff0c0\u4ee3\u8868\u6ca1\u4eba\uff0c\u8ba9\u6240\u6709\u4eba\u5750\u5230\u5176\u4ed6\u6ca1\u4eba\u7684\u5730\u65b9\u53bb\uff0c\u79fb\u52a8\u7684\u82b1\u8d39\u662f\u4f4d\u7f6e\u7684\u5dee\u503c\uff0c\u95ee\u4f60\u6700\u5c11\u7684\u82b1\u8d39\u662f\u591a\u5c11\uff1f \u9898\u89e3 \u8fd9\u9053\u9898\u7b2c\u4e00\u4e2a\u60f3\u6cd5\u662f\u628a\u7b2c\u4e00\u4e2a\u4eba\u5b89\u6392\u5230\u81ea\u5df1\u524d\u9762\u6240\u6709\u7a7a\u7684\u5730\u65b9\uff0c\u7136\u540e\u5c31\u6709\u5f88\u591a\u79cd\u5b89\u6392\u65b9\u6cd5\uff0c\u7136\u540e\u7b2c\u4e8c\u4e2a\u4eba\u518d\u5b89\u6392\u5230\u7b2c\u4e8c\u4e2a\u4eba\u524d\u9762\u7684\u5ea7\u4f4d\u4e0a\uff0c\u4e0d\u8981\u5b89\u6392\u5230\u94b1\u4e00\u4e2a\u4eba\u7684\u524d\u9762\u5c31\u884c\uff0c\u7136\u540e\u6765\u8ba1\u7b97\u6700\u5c0f\u82b1\u8d39\u3002\u597d\u50cf\u8fd9\u79cd\u5b9e\u73b0\u65b9\u6cd5\u6bd4\u8f83\u7e41\u7410\uff0c\u7136\u540e\u5c31\u91c7\u53d6\u4e0b\u9762\u7684\u65b9\u6cd5\u3002 fi\u4ee3\u8868\u524di\u4e2a\u4eba\u524dj\u4e2a\u5ea7\u4f4d\u6709\u4eba\u5750\u7684\u6700\u5c0f\u82b1\u8d39\u3002 \u5f53\u524d\u4eba\u5750\u5ea7\u4f4df[i][j] = f[i - 1][j - 1] \u5f53\u524d\u4eba\u4e0d\u5750\u5ea7\u4f4df[i][j] = f[i][j - 1] #include &lt;map&gt; #include &lt;set&gt; #include &lt;cmath&gt; #include &lt;queue&gt; #include &lt;stack&gt; #include &lt;vector&gt; #include &lt;bitset&gt; #include &lt;cstdio&gt; #include &lt;cstdlib&gt; #include &lt;cstring&gt; #include &lt;fstream&gt; #include &lt;iostream&gt; #include &lt;algorithm&gt; #include &lt;unordered_map&gt; #include &lt;unordered_set&gt; #define x first #define y second [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[40,18,7],"class_list":["post-223","post","type-post","status-publish","format-standard","hentry","category-solution","tag-dp","tag-18","tag-7"],"_links":{"self":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=223"}],"version-history":[{"count":0,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/223\/revisions"}],"wp:attachment":[{"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}