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
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
with nodes on depth , and then those on depth , and so on. Nodes on any depth are numbered
from left to right.
For example, Fig. represents a fully binary tree of maximum depth with the node numbers ,
, , ..., . Since all of the flags are initially set to be
false
, the first ball being dropped will switch
flag’s values at node , node , and node before it finally stops at position . The second ball being
dropped will switch flag’s values at node , node , and node , and stop at position . Obviously,
the third ball being dropped will switch flag’s values at node , node , and node before it stops at
position .
Fig. : An example of FBT with the maximum depth 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: D ;
and I :
Input
Contains
l
+
lines.
Line
l
the number of test cases
Line
D I test case #, two decimal numbers that are separated by one blank
...
Line
k
+
D
k
I
k
test case #
k
Line
l
+
D
l
I
l
test case #
l
Line
l
+
-
a constant ‘
-
’ representing the end of the input file
Output
Contains
l
lines.
Line the stop position
P
for the test case #
...
Line
k
the stop position
P
for the test case #
k
...
Line
l
the stop position
P
for the test case #
l
Sample Input -
Sample Output /**
题目:UVA 679 Dropping Balls
链接:https://vjudge.net/problem/UVA-679
题意:lrj算法竞赛入门经典P148. eg6-6
分析:深度为d, 小球书为n。 d=1 ○
d=2 ○ ○
d=3 ○ ○ ○ ○ 第一个小球:编号1节点关闭状态,所以向左子树走。编号1节点开启。
第二个小球:编号1节点开启状态,所以向右子树走。编号1节点关闭。
第三个小球:编号1节点关闭状态,所以向左子树走。编号1节点开启。
第四个小球:编号1节点开启状态,所以向右子树走。编号1节点关闭。
。
。
。
容易发现,对于编号1节点,要放入1-n个小球。那么奇数编号的小球往左子树走,偶数编号的小球往右子树走。
到达了左子树编号为2的节点,有若干个小球重新编号为1-m。那么又可以这样处理。 由于我们要判断第n个小球最终的叶子位置。
k表示答案,初始k=1.
if(n%2==0) k = 2*k+1, n = n/2;
else k = 2*k, n = n/2+1; if(n==1) return k; 思路:
*/ #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> P;
const int maxn = 1e5+;
const int mod = 1e9+;
int main()
{
int T;
int d, n;
LL k;
cin>>T;
while(T--)
{
scanf("%d%d",&d,&n);
k = ;
d--;
while(n>&&d--){
if(n%==) k = *k+, n = n/;
else k = *k, n = n/+;
}
printf("%lld\n",k);
}
cin>>T;
return ;
}
UVA 679 Dropping Balls 由小见大,分析思考 二叉树放小球,开关翻转,小球最终落下叶子编号。的更多相关文章
- UVA.679 Dropping Balls (二叉树 思维题)
UVA.679 Dropping Balls (二叉树 思维题) 题意分析 给出深度为D的完全二叉树,按照以下规则,求第I个小球下落在那个叶子节点. 1. 默认所有节点的开关均处于关闭状态. 2. 若 ...
- Uva 679 Dropping Balls (模拟/二叉树的编号)
题意:有一颗二叉树,深度为D,所有节点从上到下从左到右编号为1,2,3.....在结点一处放一个小球,每个节点是一个开关,初始全是关闭的,小球从顶点落下,小球每次经过开关就会把它的状态置反,现在问第k ...
- UVa 679 - Dropping Balls【二叉树】【思维题】
题目链接 题目大意: 小球从一棵所有叶子深度相同的二叉树的顶点开始向下落,树开始所有节点都为0.若小球落到节点为0的则往左落,否则向右落.并且小球会改变它经过的节点,0变1,1变0.给定树的深度D和球 ...
- 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 ...
- UVA - 679 Dropping Balls(二叉树的编号)
题意:二叉树按层次遍历从1开始标号,所有叶子结点深度相同,每个结点开关初始状态皆为关闭,小球从根结点开始下落(小球落在结点开关上会使结点开关状态改变),若结点开关关闭,则小球往左走,否则往右走,给定二 ...
- UVa OJ 679 - Dropping Balls
本题是一个二叉树问题——Perfect Binary Tree. 一个完美二叉树(PBT)的深度为D,从根结点开始,按层次遍历顺序编号为1,2,...,2D-1. 有若干个球,依次由根结点落下.当一个 ...
- Uva 679 Dropping Ballls 二叉树的编号
这个程序常规处理起来数据量很大,I可以高达2^D-1 /* ....... */ 里面的代码块据此避免了开太大的数组 做太多的循环 #include<cstdio> #include< ...
- 小游戏专场:腾讯云Game-Tech技术沙龙上海站顺利落下帷幕
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯游戏云发表于云+社区专栏 9月14日腾讯云GAME-TECH技术沙龙小游戏专场在上海顺利举办,此次技术沙龙由腾讯云的资深专家,以及 ...
随机推荐
- 收藏起来,史上最全的 MySQL 高性能优化实战总结!
转自:https://mp.weixin.qq.com/s/sRsJzFO9dPtKhovJNWN3Dg 一.前言 MySQL 对于很多 Linux 从业者而言,是一个非常棘手的问题,多数情况都是因为 ...
- linux 处理两个文件的并集,交集,计数
1. 取出两个文件的并集(重复的行只保留一份) cat file1 file2 | sort | uniq 2. 取出两个文件的交集(只留下同时存在于两个文件中的文件) cat file1 file2 ...
- NDK 调用对象属性
JNIEXPORT jbyteArrayJNICALL Java_com_lanhetech_iso8583_nativeLib_Iso8583NativeLib_pubPack8583 (JNIEn ...
- Matlab设置形状大小
x=0:10; y=2*x; plot(x,y,'-*','linewidth',0.5,'markersize',6)%%默认线宽为0.5,点大小为6 说明:调整线宽也可改变点的形状,这实际上是通过 ...
- [转] C++ try catch() throw 异常处理
原文地址 其它很多程序员一样,本书的主人公阿愚也是在初学C++时,在C++的sample代码中与异常处理的编程方法初次邂逅的,如下: // Normal program statements . ...
- 利用LogParser分析IIS日志
LogParser是微软官方出品的用于读取分析IIS日志的工具,使用类SQL语句过滤文本日志内容,并可将内容导出到csv.sqlserver作进一步分析 下载地址:http://www.micr ...
- golangWEB框架gin学习之获取post参数
原文地址:http://www.niu12.com/article/41 package main import ( "fmt" "github.com/gin-goni ...
- python socket timeout设置
需要在调用socket的connect方法之前设置settimeout(time)方法,另外在设置之后要将再次调用settimeout(None)来设置socket进入阻塞模式. 如下代码示例: so ...
- SQL注入之导出WebShell
已经听N个人过说有人已经发现SQL注入Access得到webshell的技术了,也只是听说而已,具体的细节还是不得而知. 最近在看的书中一章提到Jet的安全,然后灵光一闪,呵呵,发现了一种可以利用ac ...
- Webpack安装和配置
一.安装和配置webpack 1.全局安装webpack 这样就安装好了webpack,可以再全局通过webpack -v来查看是否安装成功. 2.先创建项目目录结构,根目录是mywebpack.进入 ...