Dropping Balls UVA - 679
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 the true 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 I-th 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:
2≤ D ≤20, and 1≤ I ≤524288.
Input
Contains l +2 lines.
Line 1 l the number of test cases
Line 2 D1 I1 test case #1, two decimal numbers that are separated by one blank
...
Line k +1 Dk Ik test case #k Line l +1 Dl Il 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
HINT
这个题目最重要的是他的结论:
- 给定一棵包含2 d 个结点(其中d 为树的高度)的完全二叉树,如果把结点从上到下从左到右编号为1,2,3……,则结点k 的左右子结点编号分别为2k 和2k +1。
- 如果使用题目中给出的编号I ,则当I 是奇数时,它是往左走的第(I +1)/2个小球;当I 是偶数时,它是往右走的第I /2个小球。
Accepted
#include <bits/stdc++.h>
using namespace std;
int main()
{
int s, m, n;
while (cin >> s&&s!=-1) {
while (s--){
cin >> m >> n;
unsigned long long int k = 1;
for (int i = 0;i < m - 1;i++)
if (n % 2) { k = k * 2;n = (n + 1) / 2; }
else { k = k * 2 + 1;n /= 2; }
cout<<k<<endl;
}
}
}
Dropping Balls UVA - 679的更多相关文章
- 小球下落(Dropping Balls, Uva 679)
题目描述 有一棵二叉树,最大深度为D,且所有的叶子深度都相同.所有结点从上到下从左到右编号为1,2,3,-,2eD-1.在结点1处放一个小球,它会往下落.每个结点上都有一个开关,初始全部关闭,当每次有 ...
- Dropping Balls UVA - 679(二叉树的遍历)
题目链接:https://vjudge.net/problem/UVA-679 题目大意:t组样例,每组包括D M 层数是D 问第M个小球落在哪个叶子节点? 每个节点有开关 刚开始全都 ...
- uva-679 Dropping Balls UVA - 679
题目大意 总共有一个深度为D的满二叉树,I个小球,每个节点具有开关,小球经过节点后节点开关会有变化,初始都关闭,若关闭往左右否则往右走 只需要循环一下每层的情况即可 代码 #include <b ...
- UVA.679 Dropping Balls (二叉树 思维题)
UVA.679 Dropping Balls (二叉树 思维题) 题意分析 给出深度为D的完全二叉树,按照以下规则,求第I个小球下落在那个叶子节点. 1. 默认所有节点的开关均处于关闭状态. 2. 若 ...
- UVA 679 Dropping Balls 由小见大,分析思考 二叉树放小球,开关翻转,小球最终落下叶子编号。
A number of K balls are dropped one by one from the root of a fully binary tree structure FBT. Each ...
- Dropping Balls (二叉树+思维)
Dropping Balls A number of K balls are dropped one by one from the root of a fully binary tree st ...
- UVa 679 【思维题】
UVA 679 紫书P148例题. 题目大意:小球从一棵所有叶子深度相同的二叉树的顶点开始向下落,树开始所有节点都为0.若小球落到节点为0的则往左落,否则向右落.并且小球会改变它经过的节点,0变1,1 ...
- UVa 679 Dropping Balls (例题 6-6)
传送门:https://uva.onlinejudge.org/external/6/p679.pdf 题意:在一颗结点带开关的完全二叉树上扔球,初始时开关为关闭状态,树的深度为D(1 <= D ...
- Uva 679 Dropping Balls
这道题如果模拟着来写,思路很简单 #include <iostream> #include <cstring> using namespace std; int T,D,I,c ...
随机推荐
- 上天的源码要不要——GitHub 热点速览 v.21.08
作者:HelloGitHub-小鱼干 前几天,"机智号" 所用的飞行软件框架 F´ 被 NASA 开源了,想看 F´ 这个嵌入式的代码不妨考虑下 Sourcetrail 这个神器, ...
- vscode 配置表
{ "git.ignoreMissingGitWarning": true, "editor.multiCursorModifier": "ctrlC ...
- python行与列显示不全
在显示数据框时添加以下代码 #显示所有列 pd.set_option('display.max_columns', None) #显示所有行 pd.set_option('display.max_ro ...
- 使用docker-compose配置mysql数据库并且初始化用户
使用docker-compose配置mysql数据库并且初始化用户 docker-compose 测试创建一个docker-compose.yml测试 以下配置了外部数据卷.外部配置文件.外部初始化 ...
- 后端程序员之路 54、go 日志库
一个朋友写的日志库 https://github.com/vizee/echo go get -u -v github.com/vizee/echo package main import ( ...
- Javascript学习,DOM对象,方法的使用
JavaScript: ECMAScript: BOM: DOM: 事件 DOM的简单学习 功能:控制html文档内容 代码:获取页面标签(元素)对象和Element document.getElem ...
- 【Arduino学习笔记04】消抖动的按键切换
"开关抖动": 由于按键是基于弹簧-阻尼系统的机械部件,所以当按下一个按键时,读到的信号并不是从低到高,而是在高低电平之间跳动几毫秒之后才最终稳定. 代码解读: 1 const i ...
- 漏洞复现-fastjson1.2.24-RCE
0x00 实验环境 攻击机:Win 10.Win Server2012 R2(公网环境,恶意java文件所在服务器) 靶机也可作为攻击机:Ubuntu18 (公网环境,docker ...
- WPF 基础 - 图片与 base64
1. base64 转图片 将 base64 转成 byte[] 将 byte[] 作为内存流保存到一个 BitmapImage 实例的流的源 把 BitmapImage 作为目标图片的 Source ...
- idea添加本地文件约束(DTD)
当我们做 xml 文件配置的时候,需要对其进行约束的配置 例如: hibernate 如果我们在联网的情况下是可以不添加配置文件约束的,红框内的 URL 会自动帮我们从网络上加载约束文件,但是没有网络 ...