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技术沙龙小游戏专场在上海顺利举办,此次技术沙龙由腾讯云的资深专家,以及 ...
随机推荐
- Asp.Net MVC part3 路由Route
路由Route路由规则Route:可以查看源代码了解一下构造方法,需要指定路由格式.默认值.处理器三个值路由数据RouteData:当前请求上下文匹配路由规则而得到的一个对象,可以在Action中通过 ...
- Linux中线程的挂起与恢复(进程暂停)
http://www.linuxidc.com/Linux/2013-09/90156.htm 今天在网上查了一下Linux中对进程的挂起与恢复的实现,相关资料少的可怜,大部分都是粘贴复制.也没有完整 ...
- zk删除node模式
检查状态 状态描述指定的znode的元数据.它包含时间戳,版本号,ACL,数据长度和子znode等细项. 语法 stat /path 示例 stat /FirstZnode 输出 [zk: local ...
- Java汉字md5值不一致问题
原文:http://blog.csdn.net/earthhour/article/details/51188437 通过main方法测试得到一个加密值,通过servlet request调用得到一个 ...
- 【java】处理时间字段 在数据库查询的时候只想要年月日,不想要时分秒 ,java中设置时间类型为年月日,java中设置Date中的时分秒为00.00.000
JDK8 中最简单的处理方式: @Test public void dateTest(){ Date now = new Date(); System.out.println(now); // jav ...
- SHAREPOINT 2013 + PROJECT 2013 资料网站
1.http://technet.microsoft.com/zh-CN/sharepoint 2\ 博客圆专业讲解 http://www.cnblogs.com/jianyus/archive/2 ...
- 所见即所得的网页设计工具 Macaw
所见即所得的网页设计工具 Macaw: 2014年最具前景的12款创新产品 1\ http://macaw.co/ 2\ http://www.kickstarter.com/projects ...
- python3发送html格式的邮件
def send_mail(to_list, sub, content, attpath): me = "*******" + "<" + mail_us ...
- LWIP的移植笔记
第一次发表博客,文章摘录于还不懂同学的专栏 lwIp的作者做了大量的工作以方便像我这种懒人移植该协议栈,基本上只需修改一个配置头文件和改写3个函数即可完成lwIP的移植.要改写的函数位于lwIP-1. ...
- HAWQ技术解析(十八) —— 问题排查
(原文地址:http://hawq.incubator.apache.org/docs/userguide/2.1.0.0-incubating/troubleshooting/Troubleshoo ...