{"id":348,"date":"2024-04-28T11:33:24","date_gmt":"2024-04-28T03:33:24","guid":{"rendered":"https:\/\/seanxd.com\/?p=348"},"modified":"2024-04-28T11:33:26","modified_gmt":"2024-04-28T03:33:26","slug":"zerojudge-a249","status":"publish","type":"post","link":"https:\/\/seanxd.com\/en\/zerojudge-a249\/","title":{"rendered":"ZeroJudge A249: Dropping Balls"},"content":{"rendered":"<h4 class=\"wp-block-heading\">UVa 00679 \u2013 Dropping Balls<\/h4>\n\n\n\n<p class=\"\">K balls are dropping one by one from the root of a complete binary tree (CBT). When a ball encounters a non-terminal node, it may go left or right subtree until it reaches a terminal node (i.e., a leaf node). The decision of whether the ball should go left or right at a non-terminal node is controlled by two values: true or false.<\/p>\n\n\n\n<p class=\"\">If the current value of the non-terminal node is false, the ball will go to the left subtree when it arrives, but the value will change to true. If the current value of the non-terminal node is true, the ball will go to the right subtree when it arrives, but the value will change to false.<\/p>\n\n\n\n<p class=\"translation-block\">Please note: <strong>Initially, all non-terminal node values are false<\/strong>. Additionally, in a complete binary tree, all nodes are sequentially numbered from top (depth = 1) to bottom, from left to right. Please refer to the following diagram.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full nfd-wb-animate nfd-wb-zoom-in-short\"><img decoding=\"async\" width=\"516\" height=\"207\" loading=\"lazy\" src=\"https:\/\/seanxd.com\/wp-content\/uploads\/2024\/04\/Dropping-Balls.gif\" alt=\"ZeroJudge A249\u984c\u76ee\u89e3\u8aaa\u7528\u5716\" class=\"wp-image-349\"\/><\/figure>\n\n\n\n<p class=\"\">For example, in the above tree with depth 4, the first ball dropping will pass through nodes 1, 2, 4, and finally land in node 8. The second ball dropping will pass through nodes 1, 3, 6, and finally land in node 12. The third ball dropping will pass through nodes 1, 2, 5, and finally land in node 10.<\/p>\n\n\n\n<p class=\"\">Given the depth D of a complete binary tree and the I-th ball dropping, please write a program to calculate the node number where the I-th ball lands in a terminal node.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sample Inputs\/Outputs<\/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>Sample Input(s)<\/th><th>Sample Output(s)<\/th><\/tr><\/thead><tbody><tr><td>The first line of input contains an integer, indicating how many sets of test data will follow.<br>Each set of test data consists of a single line containing two integers, D and I, where D represents the depth of the complete binary tree (2 &lt;= D &lt;= 20) and I represents the index of the dropping ball (1 &lt;= I &lt;= 524288).<\/td><td>For each set of test data, output a single line containing the node number where the I-th ball lands in a terminal node.<\/td><\/tr><tr><td>5<br>4  2<br>3  4<br>10  1<br>2  2<br>8  128<\/td><td>12<br>7<br>512<br>3<br>255<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Thought Process<\/h2>\n\n\n\n<p class=\"\">Run a for loop for D times. If L is even, move right (current position * 2 + 1), then L \/= 2. If L is odd, move left (current position * 2), then L = L \/ 2 + 1. Output the final position.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sample Code\uff0d<a href=\"https:\/\/zerojudge.tw\/ShowProblem?problemid=a249\" target=\"_blank\" rel=\"noopener\">ZeroJudge A249: Dropping Balls<\/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;\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 D, L;\n        cin &gt;&gt; D &gt;&gt; L;\n        int pos = 1;\n        for (int j = 0; j&lt;D; j++)\n        {\n            if (L % 2 == 0)\n            {\n                L \/= 2;\n                pos = (pos*2)+1;\n            }\n            else\n            {\n                L = (L\/2)+1;\n                pos *= 2;\n            }\n        }\n        cout &lt;&lt; pos\/2 &lt;&lt; &quot;\\n&quot;;\n    }\n}\n\n\/\/ZeroJudge A249\n\/\/Dr. SeanXD<\/code><\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u540c\u984c\uff1aUVa 00679 &#8211; Dropping Balls \u6709 K \u500b\u7403\u5f9e\u4e00\u5b8c\u6574\u4e8c\u5143\u6a39 (Full [&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":[8,13,9],"class_list":["post-348","post","type-post","status-publish","format-standard","hentry","category-uva","tag-8","tag-13","tag-9"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/348","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=348"}],"version-history":[{"count":1,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/348\/revisions"}],"predecessor-version":[{"id":350,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/348\/revisions\/350"}],"wp:attachment":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/media?parent=348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/categories?post=348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/tags?post=348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}