{"id":290,"date":"2013-05-31T11:54:14","date_gmt":"2013-05-31T03:54:14","guid":{"rendered":"http:\/\/blog.dayandcarrot.net\/?p=290"},"modified":"2013-05-31T11:54:14","modified_gmt":"2013-05-31T03:54:14","slug":"1029-median-25","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=290","title":{"rendered":"1029. Median (25)"},"content":{"rendered":"<p>Given an increasing sequence S of N integers, the\u00a0<i>median<\/i>\u00a0is the number at the middle position. For example, the median of S1={11, 12, 13, 14} is 12, and the median of S2={9, 10, 15, 16, 17} is 15. The median of two sequences is defined to be the median of the nondecreasing sequence which contains all the elements of both sequences. For example, the median of S1 and S2 is 13.<br \/>\nGiven two increasing sequences of integers, you are asked to find their median.<br \/>\n<b>Input<\/b><br \/>\nEach input file contains one test case. Each case occupies 2 lines, each gives the information of a sequence. For each sequence, the first positive integer N (&lt;=1000000) is the size of that sequence. Then N integers follow, separated by a space. It is guaranteed that all the integers are in the range of\u00a0<b>long int<\/b>.<br \/>\n<b>Output<\/b><br \/>\nFor each test case you should output the median of the two given sequences in a line.<br \/>\n<b>Sample Input<\/b><\/p>\n<pre>4 11 12 13 14\n5 9 10 15 16 17<\/pre>\n<p><b>Sample Output<\/b><\/p>\n<pre>13<\/pre>\n<p>====================================<br \/>\n\u8fd9\u9898\u5176\u5b9e\u6ca1\u4ec0\u4e48\u6280\u672f\u542b\u91cf\uff0c\u56e0\u4e3a\u8f93\u5165\u5e8f\u5217\u90fd\u5e2e\u4f60\u6392\u5e8f\u597d\u4e86\uff0c\u65f6\u95f4\u5185\u5b58\u90fd\u6ca1\u5565\u9650\u5236<br \/>\n\u8981\u6ce8\u610f\u7684\u4e00\u70b9\u5c31\u662f\uff0c\u5047\u5b9a\u8f93\u5165\u5e8f\u5217\u4e3aA,B\uff0c\u5f53A\u6216B\u67d0\u4e2a\u5df2\u7ecf\u88ab\u638f\u7a7a\u65f6\uff0c\u9700\u8981\u53e6\u5916\u5904\u7406\uff0c\u4e0d\u7136\u5c31\u4f1a\u62a5\u6bb5\u9519\u8bef\u4ec0\u4e48\u7684&#8230;<br \/>\n\u5728\u4f18\u5316\u65b9\u9762\uff0c\u5176\u5b9e\u53ef\u4ee5\u5148\u8bfb\u7b2c\u4e00\u4e2a\u5e8f\u5217\u8fdb\u961f\u5217\uff0c\u7b2c\u4e8c\u4e2a\u5e8f\u5217\u8bfb\u5165\u65f6\u76f4\u63a5\u5904\u7406\u800c\u4e0d\u5b58\u50a8\uff0c\u80fd\u6bd4\u539f\u6765\u8282\u7701\u4e00\u534a\u7a7a\u95f4\u3002<br \/>\n\u7531\u4e8e\u9898\u76ee\u6bd4\u8f83\u677e\uff0c\u6ca1\u6709\u505a\u8fd9\u65b9\u9762\u5904\u7406\u3002<br \/>\n====================================<\/p>\n<pre>\n#include <stdio.h>\n#include <queue>\nusing namespace std;\nint main()\n{\n\t\/\/\/\/\/\/\/\/\/\/\/Init\n\tqueue<long> q_a ,q_b;\n\tlong N1, N2; \/\/\u4e24\u4e2a\u5e8f\u5217\u7684\u957f\u5ea6\n\t\/\/\u4e0d\u5b58\u50a8\u7b2c\u4e8c\u4e2a\u5e8f\u5217\uff0c\u76f4\u63a5\u7528\u6765\u6bd4\u8f83\u5904\u7406\n\t\/\/\/\/\/\/\/\/\/\/\/\n\t\/\/\u8bfb\u7b2c\u4e00\u4e2a\u5e8f\u5217\n\tscanf(\"%ld\", &N1);\n\tlong temp;\n\tfor(long i=0; i<n1; i++)\n\t{\n\t\tscanf(\"%ld\", &#038;temp);\n\t\tq_a.push(temp);\n\t}\n\t\/\/\u8bfb\u7b2c\u4e8c\u4e2a\u5e8f\u5217\n\tscanf(\"%ld\", &#038;N2);\n\tlong total_length = N1 + N2;\n\tlong median_index = (total_length%2 == 0)? (total_length\/2-1):(total_length\/2);\n\tlong current_val = -1;\n\tfor(long i=0; i<n2; i++)\n\t{\n\t\tscanf(\"%ld\", &#038;temp);\n\t\tq_b.push(temp);\n\t}\n\t\/\/\u5904\u7406\n\tfor(long i=0; i<=median_index; i++)\n\t{\n\t\tif( q_a.empty() )\n\t\t{\n\t\t\tcurrent_val = q_b.front();\n\t\t\tq_b.pop();\n\t\t\tcontinue;\n\t\t}\n\t\telse if( q_b.empty())\n\t\t{\n\t\t\tcurrent_val = q_a.front();\n\t\t\tq_a.pop();\n\t\t\tcontinue;\n\t\t}\n\t\tlong m = q_a.front();\n\t\tlong n = q_b.front();\n\t\tif(m<n)\n\t\t{\n\t\t\tcurrent_val = m;\n\t\t\tq_a.pop();\n\t\t}\n\t\telse\n\t\t{\n\t\t\tcurrent_val = n;\n\t\t\tq_b.pop();\n\t\t}\n\t}\n\tprintf(\"%ld\", current_val);\n\treturn 0;\n}\n<\/pre>\n<p>\u53e6\u5916\u6d4b\u8bd5\u7ed3\u679c\u6bd4\u8f83\u6050\u6016\uff0c\u54c8\u54c8<br \/>\n\u6d4b\u8bd5\u70b9\t\u7ed3\u679c\t\u7528\u65f6(ms)\t\u5185\u5b58(kB)\t\u5f97\u5206\/\u6ee1\u5206<br \/>\n0\t\u7b54\u6848\u6b63\u786e\t0\t660\t4\/4<br \/>\n1\t\u7b54\u6848\u6b63\u786e\t0\t790\t2\/2<br \/>\n10\t\u7b54\u6848\u6b63\u786e\t320\t17270\t3\/3<br \/>\n11\t\u7b54\u6848\u6b63\u786e\t320\t17260\t1\/1<br \/>\n12\t\u7b54\u6848\u6b63\u786e\t320\t17260\t3\/3<br \/>\n13\t\u7b54\u6848\u6b63\u786e\t0\t740\t1\/1<br \/>\n2\t\u7b54\u6848\u6b63\u786e\t0\t740\t4\/4<br \/>\n3\t\u7b54\u6848\u6b63\u786e\t0\t750\t1\/1<br \/>\n4\t\u7b54\u6848\u6b63\u786e\t0\t790\t1\/1<br \/>\n5\t\u7b54\u6848\u6b63\u786e\t0\t750\t1\/1<br \/>\n6\t\u7b54\u6848\u6b63\u786e\t150\t9000\t1\/1<br \/>\n7\t\u7b54\u6848\u6b63\u786e\t160\t9000\t1\/1<br \/>\n8\t\u7b54\u6848\u6b63\u786e\t160\t9010\t1\/1<br \/>\n9\t\u7b54\u6848\u6b63\u786e\t150\t8870\t1\/1<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given an increasing sequence S of N integers, the\u00a0median\u00a0is the number at the middle position. For example, the median of S1={11, 12, 13, 14} is 12, and the median of S2={9, 10, 15, 16, 17} is 15. The median of two sequences is defined to be the median of the nondecreasing sequence which contains all [&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-290","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\/290","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=290"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/290\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}