679 - Dropping Balls

来源:互联网 发布:烟台网络党校 编辑:程序博客网 时间:2024/04/29 21:17

A number of K ballsare dropped one by one from the root of a fully binary tree structure FBT. Eachtime the ball being dropped first visits a non-terminal node. It then keepsmoving down, either follows the path of the left subtree, or follows the path ofthe right subtree, until it stops at one of the leaf nodes of FBT. To determinea ball's moving direction a flag is set up in every non-terminal node with twovalues, either false or true. Initially, all ofthe flags are false. When visiting a non-terminal node if theflag's current value at this node is false, then the ball willfirst switch this flag's value, i.e., from the false to thetrue,and then follow the left subtree of this node to keep moving down. Otherwise,it will also switch this flag's value, i.e., from the true tothe false, but will follow the right subtree of this node to keepmoving down. Furthermore, all nodes of FBT are sequentially numbered, startingat 1 with nodes on depth 1, and then those on depth 2, and so on. Nodes on anydepth are numbered from left to right.


For example, Fig. 1 represents a fully binary tree of maximum depth 4 with thenode numbers 1, 2, 3, ..., 15. Since all of the flags are initially set tobe false, the first ball being dropped will switch flag's values atnode 1, node 2, and node 4 before it finally stops at position 8. The secondball being dropped will switch flag's values at node 1, node 3, and node 6, andstop at position 12. Obviously, the third ball being dropped will switch flag'svalues at node 1, node 2, and node 5 before it stops at position 10.


Fig. 1: An example of FBT with the maximum depth 4 and sequential node numbers.


Now consider a number of test cases where two values will be given for eachtest. The first value is D, the maximum depth of FBT, and thesecond one is I, the Ith ball beingdropped. You may assume the value of I will not exceed thetotal number of leaf nodes for the given FBT.

Please write aprogram to determine the stop position P for each test case.


For each test cases the range of two parameters D and I isas below: 



Input 

Contains l+2lines.

 

Line 1           Ithe number of test cases

Line 2           

test case #1, twodecimal numbers that are separated by one blank

...                        

Line k+1

test case #k

Line l+1

test case #l

Line l+2 -1          a constant -1 representing the end of theinput file

Output 

Contains l lines.

 

Line 1           the stop position P for the test case#1

...           

Line k thestop position P for the test case #k

...           

Line l thestop position P for the test case #l

Sample Input 

5

4 2

3 4

10 1

2 2

8 128

-1

Sample Output 

12

7

512

3

255

代码:

#include<iostream>

using namespacestd;

 

int main()

{

    int test;

    cin>>test;

    while(test--)

    {

        int d,i;

        int k=1;

        cin>>d>>i;

        for(int j=1;j<=d-1;j++)

        {

            if(i%2)

            {

                k=2*k;

                i=(i+1)/2;

            }

            else

            {

                k=2*k+1;

                i=i/2;

            }

        }

        cout<<k<<endl;

    }

    cin>>test;

    return 0;

}

解析:

对于所有的完全二叉树,其任意结点K的左右子结点标号分别是2K与2K+1。由题意可知,对于任意节点,第奇数个落到该结点上的小球继续向下落到该节点的左子结点(2K),第偶数个落到该结点上的小球继续向下落到该节点的右子结点(2K+1)。因此,我们只需要知道小球降落在每个节点上的编号的奇偶性就可以判断出小球下一步的运动情况。因此,由归纳法可以推导出小球编号的递推公式。

0 0