{"id":880,"date":"2024-07-23T09:00:00","date_gmt":"2024-07-23T01:00:00","guid":{"rendered":"https:\/\/seanxd.com\/?p=880"},"modified":"2024-06-10T21:47:17","modified_gmt":"2024-06-10T13:47:17","slug":"itsa-202405-4","status":"publish","type":"post","link":"https:\/\/seanxd.com\/en\/itsa-202405-4\/","title":{"rendered":"ITSA 202405 #4: Special Sorting"},"content":{"rendered":"\n\n\n<p>\u73fe\u5728\u7d66\u60a8 N \u500b\u6574\u6578\uff0c\u8acb\u4f9d\u4ed6\u5011\u6bcf\u500b\u4f4d\u6578\u4e4b\u548c\u7531\u5c0f\u5230\u5927\u6392\u5e8f\uff0c\u82e5\u6bcf\u500b\u4f4d\u6578\u4e4b\u548c\u76f8\u540c\uff0c\u5247\u4f9d\u6574\u6578\u672c\u8eab\u7684\u6578\u503c\u7531\u5c0f\u5230\u5927\u6392\u5e8f\u3002<\/p>\n\n\n\n<p>\u5047\u8a2d\u73fe\u5728\u6709 3 \u500b\u6b63\u6574\u6578 1234\u30011258\u30015122\uff0c\u6b63\u6574\u6578 1234\uff0c\u5b83\u7684\u6bcf\u500b\u4f4d\u6578\u7684\u76f8\u52a0\u70ba1 +. 2 + 3 + 4 = 10\uff1b1258 \u70ba 16\uff1b5122 \u70ba 10\u3002\u6240\u4ee5\u6392\u5e8f\u7d50\u679c\u70ba 1234\u30015122\u30011258\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\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7bc4\u4f8b\u8f38\u5165<\/th><th>\u7bc4\u4f8b\u8f38\u51fa<\/th><\/tr><\/thead><tbody><tr><td>\u7b2c\u4e00\u884c\u662f\u4e00\u500b\u6b63\u6574\u6578 T (1 \u2264 T \u2264 10) \u4ee3\u8868\u6e2c\u8a66\u8cc7\u6599\u6709\u5e7e\u7b46\u3002<br>\u6bcf\u4e00\u7b46\u6e2c\u8a66\u8cc7\u6599\u5171 2 \u884c\u3002\u7b2c\u4e00\u884c\u662f\u4e00\u500b\u6b63\u6574\u6578 N (2 \u2264 N \u2264 10\uff09\uff0c\u5176\u4ee3\u8868\u5171\u6709 N \u500b\u6b63\u6574\u6578\u3002\u63a5\u8457\u7b2c\u4e8c\u884c\u5305\u542b\u4e86 N \u500b\u6574\u6578\uff0c\u6bcf\u500b\u6574\u6578\u7528\u7a7a\u767d\u9694\u958b\uff0c\u6bcf\u500b\u6578\u5b57\u7684\u4f4d\u6578\u662f 3 \u5230 7\u3002<\/td><td>\u8acb\u4f9d\u6392\u5e8f\u898f\u5247\uff0c\u8f38\u51fa\u6bcf\u7b46\u6e2c\u8a66\u8cc7\u6599\u7684\u6392\u5e8f\u7d50\u679c\u3002<br>\u6bcf\u884c\u8f38\u51fa\u6700\u5f8c\u5fc5\u6709\u63db\u884c\u7b26\u865f\u3002<\/td><\/tr><tr><td>5<br>3<br>1234 1258 5122<br>4<br>1725 3821 2011 1428<br>4<br>1925 2011 3210 9215<br>4<br>4145 1245 1485 1478<br>7<br>5584 12635 7894 12365 98453 56487 2134567<\/td><td>1234 5122 1258<br>2011 3821 1428 1725<br>2011 3210 1925 9215<br>1245 4145 1485 1478<br>12365 12635 5584 7894 2134567 98453 56487<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">ITSA 202405 #4 \u7bc4\u4f8b\u6e2c\u8cc7<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u984c\u601d\u8def<\/h2>\n\n\n\n<p>\u6536\u8cc7\u6599\u7684\u6642\u5019\u53ef\u4ee5\u4f7f\u7528\u5b57\u4e32\u7684\u65b9\u5f0f\u6536\u8cc7\u6599\uff0c\u7136\u5f8c\u8dd1 For\u8ff4\u5708 \u5c07\u6bcf\u4e00\u500b\u4f4d\u6578\u7684\u6578\u5b57\u9032\u884c\u76f8\u52a0\u3002<\/p>\n\n\n\n<p>\u5ba3\u544a\u4e00\u500b Vector&lt;pair&lt;int, int>> \u7528\u4f86\u6392\u5e8f\uff0c\u56e0\u70ba\u6703\u6709\u4f4d\u6578\u7e3d\u548c\u76f8\u540c\u7684\u60c5\u6cc1\uff0c\u9019\u6a23\u7684\u60c5\u6cc1\u4e0b\u5c31\u662f\u539f\u672c\u7684\u6578\u5b57\u8f03\u5c0f\u5c31\u5148\u8f38\u51fa\u3002\u5c07\u4f4d\u6578\u7e3d\u548c\u653e\u5728 Pair \u7684\u7b2c\u4e00\u500b\u6b04\u4f4d\uff0c\u539f\u672c\u7684\u5b57\u4e32\u4f7f\u7528 stoi \u8f49\u70ba\u6578\u5b57\u4e4b\u5f8c\u653e\u5728\u7b2c\u4e8c\u500b\u6b04\u4f4d\uff0c\u4e26 Push_Back \u5230 Vector \u4e2d\uff0c\u4e4b\u5f8c\u5c31\u662f sort \u4e86\u3002<\/p>\n\n\n\n<p>\u8f38\u51fa\u7684\u6642\u5019\u53ea\u9700\u8981\u5c07\u6bcf\u4e00\u500b Pair \u7684 second \u6b04\u4f4d\u8f38\u51fa\u5373\u53ef\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\u6700\u5f8c\u4e00\u500b\u8cc7\u6599\u5f8c\u9762\u662f\u4e0d\u80fd\u6709\u7a7a\u683c\u7684\uff0c\u8981\u76f4\u63a5\u63db\u884c\uff0c\u6240\u4ee5\u53ef\u4ee5\u5728\u8f38\u51fa\u7684 For\u8ff4\u5708 \u5167\u7528\u5224\u65b7\u5f0f\u5224\u65b7\u76ee\u524d\u8dd1\u5230\u7684\u6578\u5b57\u662f\u5426\u70ba N-1\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff0d<a href=\"https:\/\/e-tutor.itsa.org.tw\/mod\/programming\/view.php?a=43168\" target=\"_blank\" rel=\"noreferrer noopener\">ITSA 202405 #4\uff1a\u7279\u6b8a\u7684\u6392\u5e8f<\/a><\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-cpp\" data-lang=\"C++\"><code>#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;algorithm&gt;\nusing namespace std;\n\nint main() {\n    cin.sync_with_stdio(0);\n    cin.tie(0);\n    cout.sync_with_stdio(0);\n    cout.tie(0);\n    int T;\n    cin &gt;&gt; T;\n    for (int i = 0; i&lt;T; i++) {\n        int N;\n        cin &gt;&gt; N;\n        vector&lt;pair&lt;int, int&gt;&gt;v;\n        for (int j = 0; j&lt;N; j++) {\n            string str;\n            cin &gt;&gt; str;\n            int sum = 0;\n            for (int k = 0; k&lt;str.length(); k++) sum += str[k] - &#39;0&#39;;\n            pair&lt;int, int&gt;p;\n            p.first = sum;\n            p.second = stoi(str);\n            v.push_back(p);\n        }\n        sort(v.begin(), v.end());\n        for (int j = 0; j&lt;N; j++) {\n            cout &lt;&lt; v[j].second;\n            if (j != N-1) cout &lt;&lt; &quot; &quot;;\n        }\n        cout &lt;&lt; &quot;\\n&quot;;\n    }\n}\n\n\/\/ITSA 202405 #4\n\/\/Dr. SeanXD<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u73fe\u5728\u7d66\u60a8 N \u500b\u6574\u6578\uff0c\u8acb\u4f9d\u4ed6\u5011\u6bcf\u500b\u4f4d\u6578\u4e4b\u548c\u7531\u5c0f\u5230\u5927\u6392\u5e8f\uff0c\u82e5\u6bcf\u500b\u4f4d\u6578\u4e4b\u548c\u76f8\u540c\uff0c\u5247\u4f9d\u6574\u6578\u672c\u8eab\u7684\u6578\u503c\u7531\u5c0f\u5230\u5927\u6392\u5e8f\u3002  [&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":[36],"tags":[23,20,8,11,19,12,9],"class_list":["post-880","post","type-post","status-publish","format-standard","hentry","category-itsa","tag-pair","tag-20","tag-8","tag-11","tag-19","tag-12","tag-9"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/880","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/comments?post=880"}],"version-history":[{"count":3,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/880\/revisions"}],"predecessor-version":[{"id":883,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/880\/revisions\/883"}],"wp:attachment":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/media?parent=880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/categories?post=880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/tags?post=880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}