{"id":217,"date":"2013-04-23T21:50:02","date_gmt":"2013-04-23T13:50:02","guid":{"rendered":"http:\/\/blog.dayandcarrot.net\/?p=217"},"modified":"2013-04-23T21:50:02","modified_gmt":"2013-04-23T13:50:02","slug":"1056-mice-and-rice-25","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=217","title":{"rendered":"1056. Mice and Rice (25)"},"content":{"rendered":"<div>\u65f6\u95f4\u9650\u5236<\/div>\n<div>30 ms<\/div>\n<div>\n<div>\u5185\u5b58\u9650\u5236<\/div>\n<div>32000 kB<\/div>\n<\/div>\n<div>\n<div>\u4ee3\u7801\u957f\u5ea6\u9650\u5236<\/div>\n<div>16000 B<\/div>\n<\/div>\n<div>\n<div>\u5224\u9898\u7a0b\u5e8f<\/div>\n<div>Standard<\/div>\n<div>========<\/div>\n<div>\n<i>Mice and Rice<\/i>\u00a0is the name of a programming contest in which each programmer must write a piece of code to control the movements of a mouse in a given map. The goal of each mouse is to eat as much rice as possible in order to become a FatMouse.<br \/>\n<!--more--><br \/>\nFirst the playing order is randomly decided for N<sub>P<\/sub>\u00a0programmers. Then every N<sub>G<\/sub>\u00a0programmers are grouped in a match. The fattest mouse in a group wins and enters the next turn. All the losers in this turn are ranked the same. Every N<sub>G<\/sub>winners are then grouped in the next match until a final winner is determined.<br \/>\nFor the sake of simplicity, assume that the weight of each mouse is fixed once the programmer submits his\/her code. Given the weights of all the mice and the initial playing order, you are supposed to output the ranks for the programmers.<br \/>\n<b>Input Specification:<\/b><br \/>\nEach input file contains one test case. For each case, the first line contains 2 positive integers: N<sub>P<\/sub>\u00a0and N<sub>G<\/sub>\u00a0(&lt;= 1000), the number of programmers and the maximum number of mice in a group, respectively. If there are less than N<sub>G<\/sub>\u00a0mice at the end of the player&#8217;s list, then all the mice left will be put into the last group. The second line contains N<sub>P<\/sub>\u00a0distinct non-negative numbers W<sub>i<\/sub>\u00a0(i=0,&#8230;N<sub>P<\/sub>-1) where each W<sub>i<\/sub>is the weight of the i-th mouse respectively. The third line gives the initial playing order which is a permutation of 0,&#8230;N<sub>P<\/sub>-1 (assume that the programmers are numbered from 0 to N<sub>P<\/sub>-1). All the numbers in a line are separated by a space.<br \/>\n<b>Output Specification:<\/b><br \/>\nFor each test case, print the final ranks in a line. The i-th number is the rank of the i-th programmer, and all the numbers must be separated by a space, with no extra space at the end of the line.<br \/>\n<b>Sample Input:<\/b><\/p>\n<pre>11 3\n25 18 0 46 37 3 19 22 57 56 10\n6 0 8 7 10 5 9 1 4 2 3<\/pre>\n<p><b>Sample Output:<\/b><\/p>\n<pre>5 5 5 2 5 5 5 3 1 3 5<\/pre>\n<\/div>\n<\/div>\n<p>===================================<br \/>\n\u8fd9\u4e2a\u9898\u76ee\u4e3b\u8981\u7684\u96be\u5ea6\u5728\u65f6\u95f4\u9650\u5236\uff0c30ms\u770b\u8d77\u6765\u5f88\u5413\u4eba\uff0c\u4f46\u662f\u5176\u5b9e\u95ee\u9898\u7684\u89c4\u6a21\u4e0d\u7b97\u5927&#8230;\u6700\u591a\u624d1000\u53ea\u8001\u9f20\u561b\uff01<br \/>\n\u4e3b\u8981\u601d\u8def\u5c31\u662f\u77e9\u9635\u6570\u7ec4\u80fd\u641e\u591a\u5927\u641e\u591a\u5927\uff0c\u80fd\u7528\u9759\u6001\u7528\u9759\u6001\uff0c\u56e0\u4e3a\u5185\u5b58\u8303\u56f4\u592a\u5bbd&#8230;<br \/>\n\u4e3a\u4e86\u52a0\u5feb\u7a0b\u5e8f\u901f\u5ea6\uff0c\u5728\u5bf9\u7ec4\u5185\u7684\u8001\u9f20\u6392\u5e8f\u7684\u65f6\u5019\uff0c\u663e\u7136\u4e0d\u80fd\u4f7f\u7528\u6807\u51c6\u5e93\u91cc\u9762\u7684\u7b97\u6cd5\uff0c\u5c31\u7b97\u5feb\u901f\u6392\u5e8f\u4e5f\u6709O(nlog2n)\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u8fd9\u8fb9\u53ea\u8981\u9009\u4e00\u53ea\u6700\u80a5\u7684\u5c31\u884c\uff0c\u7528\u6700\u7b28\u7684\u5192\u6ce1\u6216\u8005\u63d2\u5165\u6392\u5e8f\u505a\u4e00\u6b21\u5c31\u884c\u4e86\uff0cO(n)\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002<br \/>\n\u53e6\u5916\u7b2c\u4e8c\u4e2a\u96be\u5ea6\u53ef\u80fd\u5728\u6bd4\u8d5b\u7ed3\u679c\u7684\u5b58\u50a8\u4e0a\uff0c\u7531\u4e8e\u65f6\u95f4\u9650\u5236\u7d27\uff0c\u6211\u5c31\u76f4\u63a5\u5f04\u4e86\u4e2a1000*1000\u7684\u77e9\u9635\u5b58\u7ed3\u679c\uff0c\u7b2c\u4e00\u884c\u5b58\u7684\u662f\u7b2c\u4e00\u8f6e\u6bd4\u8d5b\u7684\u7ed3\u679c\uff0c\u628a\u6ca1\u6709\u664b\u7ea7\u7684\u8001\u9f20\u4eec\u7684\u5e8f\u53f7\u5b58\u5728\u7b2c\u4e00\u884c\u7684\u5404\u4e2a\u5217\u4e2d\uff0c\u7528-1\u4f5c\u4e3a\u672c\u5217\u7ed3\u675f\u6807\u5fd7&#8230;\u8fd9\u6837\u4ee5\u6b64\u7c7b\u63a8\uff0c\u5f53\u67d0\u884c\u7b2c\u4e00\u4e2a\u5143\u7d20\u5c31\u662f-1\u7684\u65f6\u5019\uff0c\u6240\u6709\u80a5\u8001\u9f20\u5c31\u6bd4\u5b8c\u8d5b\u4e86\uff08\u5f53\u7136\u8fd9\u4e2a\u9996\u5217-1\u7684\u8bbe\u7f6e\u5728\u540e\u9762\u7684\u6392\u540d\u4e2d\u6ca1\u8d77\u4ec0\u4e48\u7ed3\u679c\uff0c\u56e0\u4e3a\u6709\u4e2a\u8ba1\u6570\u5668\u5df2\u7ecf\u8bb0\u4e0b\u4e00\u5171\u6bd4\u4e86\u591a\u5c11\u8f6e\u4e86\uff09<br \/>\n\u53e6\u5916\uff0c\u8bb0\u5f97cin,cout\u7528scanf,printf\u66ff\u6362\uff0c\u4e0d\u7136\u8bfb\u4e2a\u6570\u5217\u5199\u4e2a\u7ed3\u679c\u4f30\u8ba1\u5c31\u80fd\u8ba9\u7a0b\u5e8f\u8d85\u65f6\u4e86\u3002\u8fd9\u4e2a\u53ef\u80fd\u662fPAT\u8bbe\u8ba1\u7684\u4e0d\u5408\u7406\u7684\u5730\u65b9\uff0c\u5e94\u8be5\u5206\u7a0b\u5e8f\u8bed\u8a00\u6765\u9650\u5b9a\u65f6\u95f4\uff0c30ms\u8fd9\u4e48\u641e\u8ba9\u5199Java\u7684\u60c5\u4f55\u4ee5\u582a\uff1f\uff1f&#8230;<br \/>\n\u522b\u7684\u6ca1\u5565\u4e86\uff0c\u81ea\u5df1\u7684\u4ee3\u78015\u4e2a\u6d4b\u8bd5\u70b9\u90fd\u662f0ms..\u5185\u5b58\u6d88\u8017\u6216\u8bb8\u5927\u4e86\u70b9\uff0c\u54c8\u54c8<br \/>\n====================================<\/p>\n<pre>\n#include <iomanip>\nusing namespace std;\nconst int MAX_ELEM_SIZE = 1000;\nint Player_values[MAX_ELEM_SIZE];\nint Player_result[MAX_ELEM_SIZE][MAX_ELEM_SIZE] = {0}; \/\/\u6bd4\u8d5b\u7ed3\u679c,0\u8868\u793a\u8fd8\u6ca1\u6709\u51fa\u6700\u540e\u6210\u7ee9\nint contest_size = 0; \/\/\u6bcf\u573a\u6bd4\u8d5b\u7684\u603b\u4eba\u6570\nint stadium[MAX_ELEM_SIZE]; \/\/\u8d5b\u573a,\u5b58\u50a8\u5e8f\u53f7\nint sequence[MAX_ELEM_SIZE];\/\/\u6bd4\u8d5b\u987a\u5e8f\uff0c\u6ca1\u6709\u8d44\u683c\u53c2\u8d5b\u7684\u8bbe\u7f6e\u4e3a-1\nint ranks[MAX_ELEM_SIZE];\nint N_P, N_G;\nint main()\n{\n\tscanf(\"%d%d\", &N_P, &N_G);\n\tfor(int i=0; i<n_P; i++)\n\t\t\/\/\u8bfb\u53d6\u8001\u9f20\u91cd\u91cf\n\t\tscanf(\"%d\", &#038;Player_values[i]);\n\tfor(int i=0; i<n_P; i++)\n\t\t\/\/\u8bfb\u53d6\u6bd4\u8d5b\u987a\u5e8f\n\t\tscanf(\"%d\", &#038;sequence[i]);\n\t\/\/\u521d\u59cb\u5316\n\tcontest_size = N_P;\n\tint turn = 0; \/\/\u7b2c\u51e0\u8f6e\u6bd4\u8d5b\n\twhile(contest_size > 0)\n\t{\n\t\t\/\/\u4e00\u8f6e\u6bd4\u8d5b\u4e00\u4e2a\u5faa\u73af\n\t\tint max_seq=-1;\n\t\tint max_val=-1;\n\t\tint true_count = 0;\n\t\tint loser_count = 0;\n\t\tfor(int i=0; i<n_P; i++)\n\t\t{\n\t\t\tif(sequence[i] == -1) \/\/\u9047\u5230\u4e86Loser\n\t\t\t\tcontinue;\n\t\t\tif(true_count%N_G == 0)\n\t\t\t{\n\t\t\t\t\/\/\u6bcf\u7ec4\u7684\u5f00\u59cb\uff0c\u6e05\u7a7a\u4e0a\u4e00\u7ec4\u7684\u6570\u636e\n\t\t\t\tmax_seq = -1;\n\t\t\t\tmax_val = -1;\n\t\t\t}\n\t\t\tif( Player_values[ sequence[i] ] > max_val)\n\t\t\t{\n\t\t\t\tif(contest_size == 1)\n\t\t\t\t{\n\t\t\t\t\t\/\/he is the winner!\n\t\t\t\t\tPlayer_result[turn][loser_count++] = sequence[i];\n\t\t\t\t\tcontest_size = 0;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tmax_val = Player_values[ sequence[i] ];\n\t\t\t\tif( max_seq != -1)\n\t\t\t\t{\n\t\t\t\t\t\/\/\u628a\u539f\u6765\u7684\u4eba\u6807\u8bb0\u4e3aLoser\n\t\t\t\t\tPlayer_result[turn][loser_count++] = sequence[max_seq];\n\t\t\t\t\tcontest_size --;\n\t\t\t\t\tsequence[max_seq] = -1;\n\t\t\t\t}\n\t\t\t\tmax_seq = i;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t\/\/loser\n\t\t\t\tPlayer_result[turn][loser_count++] = sequence[i];\n\t\t\t\tcontest_size -- ;\n\t\t\t\tsequence[i] = -1;\n\t\t\t}\n\t\t\ttrue_count ++;\n\t\t}\n\t\tPlayer_result[turn][loser_count] = -1; \/\/\u7ec8\u6b62\u8fd9\u4e00\u884c\uff01\n\t\tturn++;\n\t}\n\tPlayer_result[turn][0] = -1;\n\t\/\/\u5f00\u59cb\u5206\u914d\u540d\u6b21\n\tint rank = 1;\n\tint r_count = 1;\n\twhile(turn --)\n\t{\n\t\tint col = 0;\n\t\trank = r_count;\n\t\twhile(true)\n\t\t{\n\t\t\tif( Player_result[turn][col] == -1)\n\t\t\t\tbreak;\n\t\t\tranks[ Player_result[turn][col] ] = rank;\n\t\t\tr_count++;\n\t\t\tcol ++;\n\t\t}\n\t}\n\tfor(int i=0; i<n_P -1; i++)\n\t\tprintf(\"%d \", ranks[i]);\n\tprintf(\"%d\", ranks[N_P-1]);\n\treturn 0;\n}\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u65f6\u95f4\u9650\u5236 30 ms \u5185\u5b58\u9650\u5236 32000 kB \u4ee3\u7801\u957f\u5ea6\u9650\u5236 16000 B \u5224\u9898\u7a0b\u5e8f Standard ======== Mice and Rice\u00a0is the name of a programming contest in which each programmer must write a piece of code to control the movements of a mouse in a given map. The goal of each mouse is to eat as much rice as possible [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[84],"class_list":["post-217","post","type-post","status-publish","format-standard","hentry","category-study","tag-pat"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=217"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/217\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}