[2016-02-08][UVA][679][Dropping Balls]

UVA - 679

Dropping Balls

Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

Submit Status

Description

A number of K balls are dropped one by one from the root of a fully binary tree structure FBT. Each time the ball being dropped first visits a non-terminal node. It then keeps moving down, either follows the path of the left subtree, or follows the path of the right subtree, until it stops at one of the leaf nodes of FBT. To determine a ball‘s moving direction a flag is set up in every non-terminal node with two values, either false or true. Initially, all of the flags are false. When visiting a non-terminal node if the flag‘s current value at this node is false, then the ball will first switch this flag‘s value, i.e., from the false to the true, 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 thetrue to the false, but will follow the right subtree of this node to keep moving down. Furthermore, all nodes of FBT are sequentially numbered, starting at 1 with nodes on depth 1, and then those on depth 2, and so on. Nodes on any depth are numbered from left to right.

For example, Fig. 1 represents a fully binary tree of maximum depth 4 with the node numbers 1, 2, 3, ..., 15. Since all of the flags are initially set to be false, the first ball being dropped will switch flag‘s values at node 1, node 2, and node 4 before it finally stops at position 8. The second ball being dropped will switch flag‘s values at node 1, node 3, and node 6, and stop at position 12. Obviously, the third ball being dropped will switch flag‘s values 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 each test. The first value is D, the maximum depth of FBT, and the second one is I, the Ith ball being dropped. You may assume the value of I will not exceed the total number of leaf nodes for the given FBT.

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

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

Input

Contains l+2 lines.

 Line 1 I the number of test cases
Line 2  test case #1, two decimal numbers that are separatedby 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 the input file 

Output

Contains l lines.

 Line 1 		 the stop position P for the test case #1
...
Line k the stop position P for the test case #k
...
Line l the stop 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
  • 时间:2016-02-07 20:12:35 星期日
  • 题目编号:UVA 679

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

#include <vector>

#include <list>

#include <map>

#include <set>

#include <deque>

#include <queue>

#include <stack>

#include <bitset>

#include <algorithm>

#include <functional>

#include <numeric>

#include <utility>

#include <sstream>

#include <iostream>

#include <iomanip>

#include <cstdio>

#include <cmath>

#include <cstdlib>

#include <cctype>

#include <string>

#include <cstring>

#include <cstdio>

#include <cmath>

#include <cstdlib>

#include <ctime>

using namespace std;

typedef long long LL;

#define CLR(x,y) memset((x),(y),sizeof((x)))

#define FOR(x,y,z) for(int (x)=(y);(x)<(z);(x)++)

#define FORD(x,y,z) for(int (x)=(y);(x)>=(z);(x)--)

#define FOR2(x,y,z) for((x)=(y);(x)<(z);(x)++)

#define FORD2(x,y,z) for((x)=(y);(x)>=(z);(x)--)

int main(){

        int t;

        while(~scanf("%d",&t) && ~t){

                while(t--){

                        int d,i,k = 1;

                        scanf("%d%d",&d,&i);

                        FOR(jj,0,d - 1){

                                k = (k<<1)+1-(i&1?1:0);

                                i=(i>>1)+(i&1?1:0);

                        }

                        printf("%d\n",k);

                }

        }

    return 0;

}

来自为知笔记(Wiz)

时间: 2024-10-09 18:14:06

[2016-02-08][UVA][679][Dropping Balls]的相关文章

UVa 679 Dropping Balls (例题 6-6)

传送门:https://uva.onlinejudge.org/external/6/p679.pdf 题意:在一颗结点带开关的完全二叉树上扔球,初始时开关为关闭状态,树的深度为D(1 <= D <= 20), 根结点为1(节点从1开始到2D-1),开关为关闭向左子结点走,否则往右子结点走,每到一个结点改变该结点开关状态.问第 I 颗球落在哪. 当 I 是奇数时, 它是往当前结点的左子结点走的第 (I + 1) / 2 颗球; 当 I 是偶数时, 它是往当前结点的右子结点走的第 I / 2 颗

UVA - 679 Dropping Balls 规律

题目大意:给出一棵完全二叉树,每个节点上都有一个开关,刚开始开关都是开的,球经过该节点后开关就关上了(根节点除外).如果球经过该节点的兄弟节点,那么该节点的开关就又开了 现在往根节点放球,优先选择左边的节点走,如果左边的节点的开关关了,就选择右边的.球落到叶节点后就继续另一颗球,问第n颗球落到了哪个叶节点上 解题思路:其实这题蛮水的,因为是二叉树,所以可以由球的奇偶行来判断球往哪个分支走,如果n % 2 == 0就表示第n颗往右边走了,反之就是往左边走了,以此类推即可推出最后一颗球落到哪了 #i

UVa 679 - Dropping Balls

称号:有一个完整的二叉树,每个节点是一个开关,最初的全封闭,球从顶点丢弃. 每次通过开关球将将其状态反转.现在先问k球落到d当层交换机经过号. 分析:进制编码.经过模拟几次能够看出,球会让开关形成连续二进制数的表示(根是低位). 当放入第k个球时.开关状态正好是二进制的k.利用模2的余数推断走向就可以. 说明:观察规律模拟处理就可以. #include <iostream> #include <cstdlib> #include <cstring> #include &

UVa 679 - Dropping Balls【二叉树】【思维题】

题目链接 题目大意: 小球从一棵所有叶子深度相同的二叉树的顶点开始向下落,树开始所有节点都为0.若小球落到节点为0的则往左落,否则向右落.并且小球会改变它经过的节点,0变1,1变0.给定树的深度D和球的个数I,问第I个小球会最终落到哪个叶子节点. 解题思路: 完全二叉树有一个重要的性质:对于任意一个节点k,其左节点.右节点的编号分别为2k和2k+1 对于根节点,很容易知道当球的编号为奇数时,球落入左子树,偶数时落在右子树.这样其实对于其它节点看成根节点时也是一样的.比如对于第7个球,为奇数,是第

679 - Dropping Balls

Dropping Balls PS:因为该题排版较麻烦,这里给出OJ网址:UVa679 - Dropping Balls 有一棵二叉树,最大深度为D,且所有叶子的深度都相同.所有结点从上到下从左到右编号为1, 2, 3,-, 2D-1.在结点1处放一个小球,它会往下落.每个内结点上都有一个开关,初始全部关闭,当每次有小球落到一个开关上时,状态都会改变.当小球到达一个内结点时,如果该结点上的开关关闭,则往左走,否则往右走,直到走到叶子结点,如图6-2所示. 一些小球从结点1处依次开始下落,最后一个

2016.02.08 值此新年,追梦想

虽然是新年,但已经到了公历的二月份,六分之一已过,离今年的目标有多远?问起自己的目标,一脸茫然,到底目标是什么?梦想又是什么? 每一个季节都需要目标,奋斗的过程需要目标来砥砺自己,丧失目标就是丧失勇气. 梦想不应该在世俗中销声匿迹,相反,梦想应该摆脱世俗的牵绊,作为人生的灯塔. 针对于学习和工作,首要的目标是成为浙江大学优秀的毕业生,让自己的研究生生涯圆满落幕,这是最基本的,也是最需要关注的.对于在蘑菇街的工作和学习,希望自己能够鼓足勇气来学习.不断学习,要知道,只有自己的技术.理论层次高了,才

UVa679:Dropping Balls

Dropping Balls A number of K balls are dropped one by one from the root of a fully binary tree structure FBT. Each time the ball being dropped first visits a non-terminal node. It then keeps moving down, either follows the path of the left subtree, o

2016/02/21 codes

var Class = { create:function(){ var parent = null,properties = $A(arguments); if(Object.isFunction(properties[0])) parent = properties.shift(); function kclass(){ this.initialize.apply(this.arguments); } Object.extend(kclass,Class.Methods); kclass.s

FFMpeg ver 20160219-git-98a0053 滤镜中英文对照 2016.02.21 by 1CM

FFMpeg ver 20160219-git-98a0053 滤镜中英文对照 2016.02.21 by 1CM T.. = Timeline support 支持时间轴 .S. = Slice threading 分段线程 ..C = Command support 支持命令传送 A = Audio input/output 音频 输入/输出 V = Video input/output 视频 输入/输出 N = Dynamic number and/or type of input/out