{"id":633,"date":"2024-07-04T09:00:00","date_gmt":"2024-07-04T01:00:00","guid":{"rendered":"https:\/\/seanxd.com\/?p=633"},"modified":"2024-05-29T16:52:52","modified_gmt":"2024-05-29T08:52:52","slug":"zerojudge-e564","status":"publish","type":"post","link":"https:\/\/seanxd.com\/zh\/zerojudge-e564\/","title":{"rendered":"ZeroJudge E564: Team Queue"},"content":{"rendered":"\n\n\n<h4 class=\"wp-block-heading\">\u540c\u984c\uff1aUVa 00540 &#8211; Team Queue<\/h4>\n\n\n\n<p class=\"\">\u300cQueues\u300d\u548c\u300cPriority Queues\u300d\u662f\u5927\u90e8\u4efd\u7684\u4eba\u90fd\u77e5\u9053\u7684\u8cc7\u6599\u7d50\u69cb\u3002<br>\u7136\u800c\uff0c\u5118\u7ba1\u300cTeam Queue\u300d\u5728\u65e5\u5e38\u751f\u6d3b\u4e2d\u7d93\u5e38\u767c\u751f\uff0c\u4f46\u4e26\u4e0d\u662f\u773e\u6240\u5468\u77e5\u3002<br>\u5728\u300cTeam Queue\u300d\u4e2d\uff0c<strong>\u6bcf\u500b\u5143\u7d20\u90fd\u5c6c\u65bc\u4e00\u500b Team<\/strong>\u3002\u5982\u679c\u67d0\u500b\u5143\u7d20\u9032\u5165 Queue\uff0c\u5b83\u5c07\u9996\u5148\u5f9e\u982d\u5230\u5c3e\u641c\u7d22 Queue\uff0c\u4ee5\u6aa2\u67e5\u5176 teammates (\u540c\u4e00 Team \u7684\u5143\u7d20) \u662f\u5426\u5df2\u7d93\u5728 Queue \u4e2d\u3002<br>\u5982\u679c\u662f\uff0c\u5b83\u5c07\u6392\u5728\u5176Team\u7684\u6700\u5f8c\u4e00\u500b\u3002\u5982\u679c\u4e0d\u662f\uff0c\u5b83\u5c07\u6392\u5728 Queue \u6700\u5f8c\u9762\u3002<br>Dequeuing \u7684\u65b9\u5f0f\u8207\u666e\u901a Queue \u4e2d\u4e00\u6a23\uff1a\u5143\u7d20\u6309\u7167\u5728 Team Queue \u4e2d\u51fa\u73fe\u7684\u9806\u5e8f\u5f9e\u982d\u5230\u5c3e\u9032\u884c\u8655\u7406\u3002<br>\u60a8\u7684\u4efb\u52d9\u662f<strong>\u5beb\u4e00\u500b\u7a0b\u5f0f\u6a21\u64ec Team Queue<\/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><strong>EOF \u8f38\u5165<\/strong>\uff0c\u6bcf\u7d44\u6e2c\u8cc7\u7b2c\u4e00\u884c\u70ba\u4e00\u500b\u6574\u6578 T (1 \u2264 T \u2264 1000)\uff0cT \u4ee3\u8868\u6709\u5e7e\u500b Team\u3002<br>\u5982\u679c T = 0 \u4ee3\u8868\u8f38\u5165\u7d50\u675f\u3002<br>\u63a5\u4e0b\u4f86 T \u884c\uff0c\u6bcf\u884c\u7b2c\u4e00\u500b\u6574\u6578 N (1 \u2264 N \u2264 1000)\uff0c\u4ee3\u8868\u8a72 Team \u7684\u6210\u54e1\u6578\u91cf\u3002<br>\u4e4b\u5f8c\u8ddf\u8457 N \u500b\u6574\u6578 X<sub>i<\/sub> (0 \u2264 X<sub>i<\/sub> \u2264 999999)\u3002<br>\u63a5\u4e0b\u4f86\u70ba\u82e5\u5e72\u500b\u6307\u4ee4\uff0c\u4ee5\u4e0b\u70ba\u6307\u4ee4\u5217\u8868\uff1a<br><strong>ENQUEUE X<\/strong>: \u82e5 X \u6240\u5c6c\u7684\u5718\u968a\u6210\u54e1\u5df2\u5728Queue\u5167\uff0c\u5c31\u6392\u9032 Team \u5167\u7684\u6700\u5f8c\u4e00\u500b\u3002\u82e5\u6c92\u6709\uff0c\u5247\u6392\u9032 Queue \u7684\u6700\u5f8c\u4e00\u500b\u3002<br><strong>DEQUEUE<\/strong>: \u5c07Queue \u6700\u524d\u9762\u7684\u5143\u7d20\u79fb\u9664\uff0c\u4e26\u8f38\u51fa\u6b64\u5143\u7d20\u3002<br><strong>STOP<\/strong>: \u7d50\u675f\u3002<br>\u6ce8\u610f\uff1a\u5be6\u73fe\u7684 Team Queue \u9700\u6709\u6548\u7387\uff0c\u5143\u7d20\u7684 Enqueing \u548c Dequeuing \u90fd\u61c9\u50c5\u82b1\u8cbb\u6046\u5b9a\u7684\u6642\u9593\u3002<\/td><td>\u5c0d\u65bc\u6bcf\u7d44\u6e2c\u8cc7\uff0c\u8f38\u51fa\u300cScenario #k\u300d\uff0c\u5176\u4e2d k \u662f\u6e2c\u8cc7\u7684\u7de8\u865f\u3002<br>\u63a5\u8457\u5c0d\u65bc\u6bcf\u500b\u300cDEQUEUE\u300d\u6307\u4ee4\uff0c\u5c07\u5df2\u51fa Queue \u7684\u5143\u7d20\u8f38\u51fa\u3002<br>\u5728<strong>\u6bcf\u7d44\u6e2c\u8cc7\u4e4b\u5f8c\u7686\u8981\u8f38\u51fa\u7a7a\u767d\u884c<\/strong>\u3002<\/td><\/tr><tr><td>2<br>3 101 102 103<br>3 201 202 203<br>ENQUEUE 101<br>ENQUEUE 201<br>ENQUEUE 102<br>ENQUEUE 202<br>ENQUEUE 103<br>ENQUEUE 203<br>DEQUEUE<br>DEQUEUE<br>DEQUEUE<br>DEQUEUE<br>DEQUEUE<br>DEQUEUE<br>STOP<br>2<br>5 259001 259002 259003 259004 259005<br>6 260001 260002 260003 260004 260005 260006<br>ENQUEUE 259001<br>ENQUEUE 260001<br>ENQUEUE 259002<br>ENQUEUE 259003<br>ENQUEUE 259004<br>ENQUEUE 259005<br>DEQUEUE<br>DEQUEUE<br>ENQUEUE 260002<br>ENQUEUE 260003<br>DEQUEUE<br>DEQUEUE<br>DEQUEUE<br>DEQUEUE<br>STOP<br>0<\/td><td>Scenario #1<br>101<br>102<br>103<br>201<br>202<br>203<br>Scenario #2<br>259001<br>259002<br>259003<br>259004<br>259005<br>260001<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">ZeroJudge E564 \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 class=\"\">\u5ba3\u544a\u4e00\u500b map&lt;string, int> \u4f86\u5b58\u6bcf\u4e00\u500b\u7de8\u865f\u7684\u968a\u4f0d\u662f\u54ea\u500b\uff0cTeam Queue \u53ef\u4ee5\u4f7f\u7528\u4e00\u500b\u4e8c\u7dad\u9663\u5217\u4f86\u5be6\u73fe\u3002<\/p>\n\n\n\n<p class=\"\">\u9700\u8981\u5ba3\u544a\u4e00\u500b map&lt;int, int> \u4f86\u5b58\u76ee\u524d\u6bcf\u4e00\u500b\u968a\u4f0d\u5728\u4e8c\u7dad\u9663\u5217\u4e2d\u7684\u4f4d\u7f6e\uff0c\u5982\u679c\u9032\u884c ENQUEUE \u7684\u6642\u5019\u8a72\u968a\u4f0d\u7684\u503c\u70ba 0\uff0c\u5247\u5c07\u4e00\u500b\u65b0\u7684\u4e00\u7dad\u9663\u5217\u5305\u542b\u5b57\u4e32 Push_Back \u5230\u4e8c\u7dad\u9663\u5217\u4e2d\uff0c\u4e26\u4e14\u5c07\u8a72\u968a\u4f0d\u7684 map \u503c\u8a2d\u5b9a\u70ba\u4e8c\u7dad\u9663\u5217\u7684\u5927\u5c0f\u4e5f\u5c31\u662f\u6700\u5f8c\u9762\u3002<\/p>\n\n\n\n<p class=\"\">\u9032\u884c DEQUEUE \u6642\u5c31\u5c07\u4e8c\u7dad\u9663\u5217\u7684 [0][0] \u8f38\u51fa\u4e26\u4e14\u5c07\u5176\u522a\u9664\uff0c\u6bcf\u6b21\u522a\u9664\u90fd\u8981\u5224\u65b7\u7b2c\u4e00\u500b\u4e00\u7dad\u9663\u5217\u9084\u6709\u6c92\u6709\u8cc7\u6599\uff0c\u5982\u679c\u6c92\u6709\u7684\u8a71\u5c31\u5c07\u9019\u500b\u4e00\u7dad\u9663\u5217\u522a\u9664\uff0c\u4e26\u4e14\u8981\u66f4\u65b0 Map \u4e2d\u7684\u6240\u6709\u8cc7\u6599\uff0c\u53ef\u4ee5\u4f7f\u7528 for(auto it:Map) \u4f86\u66f4\u65b0\u8cc7\u6599\u3002\u5982\u679c it.second == 1 \u5c31\u4ee3\u8868\u9019\u662f\u525b\u525b\u522a\u6389\u7684\u4e00\u7dad\u9663\u5217\uff0c\u5c07\u5176\u8a2d\u70ba 0\uff0c\u5982\u679c\u4e0d\u662f 1 \u4e5f\u4e0d\u662f 0 \u5c31\u5c07 Map \u503c -1\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff0d<a href=\"https:\/\/zerojudge.tw\/ShowProblem?problemid=e564\" target=\"_blank\" rel=\"noreferrer noopener\">ZeroJudge E564: Team Queue<\/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;map&gt;\n#include &lt;vector&gt;\nusing namespace std;\n\nint main() {\n    cin.sync_with_stdio(0);\n    cin.tie(0);\n    int T, count = 1;\n    while (cin &gt;&gt; T && T != 0) {\n        map&lt;string, int&gt;MAP;\n        for (int i = 0; i&lt;T; i++) {\n            int N;\n            cin &gt;&gt; N;\n            for (int j = 0; j&lt;N; j++) {\n                string str;\n                cin &gt;&gt; str;\n                MAP[str] = i;\n            }\n        }\n        string str;\n        vector&lt;vector&lt;string&gt;&gt;v;\n        map&lt;int, int&gt;hi;\n        cout &lt;&lt; &quot;Scenario #&quot; &lt;&lt; count &lt;&lt; &quot;\\n&quot;;\n        count++;\n        while (cin &gt;&gt; str && str != &quot;STOP&quot;) {\n            if (str == &quot;ENQUEUE&quot;) {\n                string s;\n                cin &gt;&gt; s;\n                int index = hi[MAP[s]];\n                if (index == 0) {\n                    vector&lt;string&gt;tmp;\n                    tmp.push_back(s);\n                    v.push_back(tmp);\n                    hi[MAP[s]] = v.size();\n                }\n                else v[index-1].push_back(s);\n                continue;\n            }\n            cout &lt;&lt; v[0][0] &lt;&lt; &quot;\\n&quot;;\n            v[0].erase(v[0].begin());\n            if (v[0].empty()) {\n                v.erase(v.begin());\n                for (auto it:hi) {\n                    if (it.second == 1) hi[it.first] = 0;\n                    else if (it.second != 0) hi[it.first]--;\n                }\n            }\n        }\n        cout &lt;&lt; &quot;\\n&quot;;\n    }\n}\n\n\/\/ZeroJudge E564\n\/\/Dr. SeanXD<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u540c\u984c\uff1aUVa 00540 &#8211; Team Queue \u300cQueues\u300d\u548c\u300cPriority Queu [&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":[14,20,22,8,11,9],"class_list":["post-633","post","type-post","status-publish","format-standard","hentry","category-uva","tag-map","tag-20","tag-22","tag-8","tag-11","tag-9"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/posts\/633","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=633"}],"version-history":[{"count":2,"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/posts\/633\/revisions"}],"predecessor-version":[{"id":635,"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/posts\/633\/revisions\/635"}],"wp:attachment":[{"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/media?parent=633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/categories?post=633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/tags?post=633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}