{"id":215,"date":"2024-04-26T20:39:44","date_gmt":"2024-04-26T12:39:44","guid":{"rendered":"https:\/\/seanxd.com\/?p=215"},"modified":"2024-04-26T20:39:46","modified_gmt":"2024-04-26T12:39:46","slug":"zerojudge-e561","status":"publish","type":"post","link":"https:\/\/seanxd.com\/zh\/zerojudge-e561\/","title":{"rendered":"ZeroJudge E561: Train Swapping"},"content":{"rendered":"\n\n\n<h4 class=\"wp-block-heading\">\u540c\u984c\uff1aUVa 00299 &#8211; Train Swapping<\/h4>\n\n\n\n<p class=\"\">\u5728\u8001\u820a\u7684\u706b\u8eca\u7ad9\uff0c\u60a8\u4e5f\u8a31\u6703\u9047\u5230\u5c11\u6578\u50c5\u5b58\u7684&#8221;\u8eca\u7bb1\u7f6e\u63db\u54e1&#8221;\u3002<br>&#8220;\u8eca\u7bb1\u7f6e\u63db\u54e1&#8221;\u662f\u9435\u8def\u90e8\u9580\u7684\u54e1\u5de5\uff0c\u4e3b\u8981\u5de5\u4f5c\u5c31\u662f\u91cd\u65b0\u6392\u5217\u706b\u8eca\u8eca\u5ec2\u3002<br>\u4e00\u65e6\u4ee5\u6700\u4f73\u9806\u5e8f\u6392\u5217\u4e86\u8eca\u5ec2\uff0c\u6240\u6709\u706b\u8eca\u53f8\u6a5f\u8981\u505a\u7684\u5c31\u662f\u5c07\u8eca\u5ec2\u9010\u4e00\u5378\u4e0b\u5373\u53ef\u3002<br>&#8220;\u8eca\u7bb1\u7f6e\u63db\u54e1&#8221;\u6e90\u81ea\u5728\u9435\u8def\u6a4b\u9644\u8fd1\u7684\u8eca\u7ad9\u4e2d\u57f7\u884c\u6b64\u4efb\u52d9\u7684\u7b2c\u4e00\u4eba\u3002<br>\u9019\u5ea7\u6a4b\u4e26\u4e0d\u6703\u5782\u76f4\u6253\u958b\uff0c\u800c\u662f\u7e5e\u8457\u6cb3\u4e2d\u592e\u7684\u4e00\u6839\u652f\u67f1\u65cb\u8f49\u3002\u5c07\u6a4b\u65cb\u8f4990\u5ea6\u5f8c\uff0c\u8239\u96bb\u5c31\u80fd\u5411\u5de6\u6216\u5411\u53f3\u99db\u904e\u3002<br>\u7b2c\u4e00\u4f4d&#8221;\u8eca\u7bb1\u7f6e\u63db\u54e1&#8221;\u767c\u73fe\uff0c\u9019\u5ea7\u6a4b\u6700\u591a\u53ef\u4ee5\u5728\u5176\u4e0a\u904b\u884c\u5169\u500b\u8eca\u5ec2\uff0c\u901a\u904e\u5c07\u6a4b\u65cb\u8f49180\u5ea6\uff0c\u8eca\u5ec2\u5c31\u80fd\u5207\u63db\u4f4d\u7f6e\u3002<br>(\u7f3a\u9ede\u662f\u8eca\u5ec2\u9762\u5411\u76f8\u53cd\u7684\u65b9\u5411\uff0c\u4f46\u662f\u706b\u8eca\u8eca\u5ec2\u53ef\u4ee5\u4ee5\u4efb\u4f55\u4e00\u7a2e\u65b9\u5f0f\u79fb\u52d5\uff0c\u6240\u4ee5\u6c92\u5dee\uff09\u3002<br>\u73fe\u5728\u5e7e\u4e4e\u6240\u6709\u7684&#8221;\u8eca\u7bb1\u7f6e\u63db\u54e1&#8221;\u90fd\u5df2\u7d93\u6dd8\u6c70\u4e86\uff0c\u9435\u8def\u516c\u53f8\u5e0c\u671b\u5c07\u5176\u64cd\u4f5c\u81ea\u52d5\u5316\u3002<br>\u4f60\u7684\u4efb\u52d9\u5c31\u662f\u5beb\u4e00\u500b\u7a0b\u5f0f\uff0c\u8a72\u7a0b\u5f0f\u8981<strong>\u8a08\u7b97\u6700\u5c11\u9700\u8981\u4ea4\u63db\u5e7e\u6b21\u5169\u500b\u76f8\u9130\u8eca\u5ec2\uff0c\u624d\u80fd\u5c07\u6240\u6709\u8eca\u5ec2\u4f9d\u5e8f\u6392\u597d<\/strong>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7bc4\u4f8b\u6e2c\u8cc7<\/h2>\n\n\n\n<figure class=\"wp-block-table nfd-wb-animate nfd-wb-fade-in-bottom-short\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7bc4\u4f8b\u8f38\u5165<\/th><th>\u7bc4\u4f8b\u8f38\u51fa<\/th><\/tr><\/thead><tbody><tr><td>\u8f38\u5165\u7684\u7b2c\u4e00\u884c\u5305\u542b\u4e00\u500b\u6574\u6578 N\uff0cN \u4ee3\u8868\u6e2c\u8cc7\u6578\u91cf\u3002<br>\u6bcf\u7d44\u6e2c\u8cc7\u7684\u7b2c\u4e00\u884c\u5305\u542b\u4e00\u500b\u6574\u6578 <strong>L<\/strong> (0 \u2264 L \u2264 50)\uff0c<strong>L \u4ee3\u8868\u706b\u8eca\u7684\u9577\u5ea6<\/strong>\u3002<br>\u7b2c\u4e8c\u884c\u5305\u542b\u6578\u5b57 1 \u5230 L \u7684\u6392\u5217\uff0c\u8868\u793a\u706b\u8eca\u8eca\u5ec2\u7684\u7576\u524d\u9806\u5e8f\u3002<br>\u9700\u8981\u5c07\u706b\u8eca\u8eca\u5ec2\u4f9d\u7167\u7de8\u865f 1 \u5230 L \u7684\u9806\u5e8f\u6392\u597d\u3002<\/td><td>\u5c0d\u65bc\u6bcf\u7d44\u6e2c\u8cc7\uff0c\u8acb\u8f38\u51fa\uff1a<br>\u300cOptimal train swapping takes S swaps.\u300d\uff0cS \u4ee3\u8868<strong>\u6700\u5c11\u4ea4\u63db\u6b21\u6578<\/strong>\uff0c\u53ef\u5ffd\u7565\u55ae\u6578\u4e0d\u52a0 s \u4e4b\u6587\u6cd5\u898f\u5247\u3002<\/td><\/tr><tr><td>3<br><strong>3<\/strong><br>1  3  2<br><strong>4<\/strong><br>4  3  2  1<br><strong>2<\/strong><br>2  1<\/td><td>Optimal train swapping takes 1 swaps.<br>Optimal train swapping takes 6 swaps.<br>Optimal train swapping takes 1 swaps.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u984c\u601d\u8def<\/h2>\n\n\n\n<p class=\"\">\u53ef\u4ee5\u4f7f\u7528 <strong>Bubble Sort (\u6c23\u6ce1\u6392\u5e8f\u6cd5)<\/strong>\uff0c\u6bcf\u6b21\u9032\u884c <strong>Swap \u7684\u6642\u5019\u5c31\u5c07\u7b54\u6848\u7684\u8b8a\u6578+1<\/strong>\uff0c\u6700\u5f8c\u8f38\u51faSwap\u7684\u6b21\u6578\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff0d<a href=\"https:\/\/zerojudge.tw\/ShowProblem?problemid=e561\" target=\"_blank\" rel=\"noreferrer noopener\">ZeroJudge E561: Train Swapping<\/a><\/h3>\n\n\n\n<div class=\"hcb_wrap nfd-wb-animate nfd-wb-reveal-right nfd-delay-50\"><pre class=\"prism line-numbers lang-cpp\" data-lang=\"C++\"><code>#include &lt;iostream&gt;\n#include &lt;vector&gt;\nusing namespace std;\n\nint main() {\n    cin.sync_with_stdio(0);\n    cin.tie(0);\n    int N;\n    cin &gt;&gt; N;\n    for (int i = 0; i&lt;N; i++)\n    {\n        int len;\n        cin &gt;&gt; len;\n        vector&lt;int&gt;num;\n        for (int j = 0; j&lt;len; j++)\n        {\n            int tmp;\n            cin &gt;&gt; tmp;\n            num.push_back(tmp);\n        }\n        int ans = 0;\n        for (int j = 0; j&lt;len; j++)\n        {\n            for (int k = 0; k&lt;len-1; k++)\n            {\n                if (num[k] &gt; num[k+1])\n                {\n                    swap(num[k], num[k+1]);\n                    ans++;\n                }\n            }\n        }\n        cout &lt;&lt; &quot;Optimal train swapping takes &quot; &lt;&lt; ans &lt;&lt; &quot; swaps.\\n&quot;;\n    }\n}\n\n\/\/ZeroJudge E561\n\/\/Dr. SeanXD<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u540c\u984c\uff1aUVa 00299 &#8211; Train Swapping \u5728\u8001\u820a\u7684\u706b\u8eca\u7ad9\uff0c\u60a8\u4e5f\u8a31\u6703\u9047\u5230\u5c11\u6578\u50c5\u5b58\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","footnotes":""},"categories":[18],"tags":[20,8,19,9],"class_list":["post-215","post","type-post","status-publish","format-standard","hentry","category-uva","tag-20","tag-8","tag-19","tag-9"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/posts\/215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/comments?post=215"}],"version-history":[{"count":2,"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/posts\/215\/revisions"}],"predecessor-version":[{"id":217,"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/posts\/215\/revisions\/217"}],"wp:attachment":[{"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/media?parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/categories?post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/tags?post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}