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 由小见大,分析思考 二叉树放小球,开关翻转,小球最终落下叶子编号。的更多相关文章

  1. UVA.679 Dropping Balls (二叉树 思维题)

    UVA.679 Dropping Balls (二叉树 思维题) 题意分析 给出深度为D的完全二叉树,按照以下规则,求第I个小球下落在那个叶子节点. 1. 默认所有节点的开关均处于关闭状态. 2. 若 ...

  2. Uva 679 Dropping Balls (模拟/二叉树的编号)

    题意:有一颗二叉树,深度为D,所有节点从上到下从左到右编号为1,2,3.....在结点一处放一个小球,每个节点是一个开关,初始全是关闭的,小球从顶点落下,小球每次经过开关就会把它的状态置反,现在问第k ...

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

    题目链接 题目大意: 小球从一棵所有叶子深度相同的二叉树的顶点开始向下落,树开始所有节点都为0.若小球落到节点为0的则往左落,否则向右落.并且小球会改变它经过的节点,0变1,1变0.给定树的深度D和球 ...

  4. UVa 679 Dropping Balls (例题 6-6)

    传送门:https://uva.onlinejudge.org/external/6/p679.pdf 题意:在一颗结点带开关的完全二叉树上扔球,初始时开关为关闭状态,树的深度为D(1 <= D ...

  5. Uva 679 Dropping Balls

    这道题如果模拟着来写,思路很简单 #include <iostream> #include <cstring> using namespace std; int T,D,I,c ...

  6. UVA - 679 Dropping Balls(二叉树的编号)

    题意:二叉树按层次遍历从1开始标号,所有叶子结点深度相同,每个结点开关初始状态皆为关闭,小球从根结点开始下落(小球落在结点开关上会使结点开关状态改变),若结点开关关闭,则小球往左走,否则往右走,给定二 ...

  7. UVa OJ 679 - Dropping Balls

    本题是一个二叉树问题——Perfect Binary Tree. 一个完美二叉树(PBT)的深度为D,从根结点开始,按层次遍历顺序编号为1,2,...,2D-1. 有若干个球,依次由根结点落下.当一个 ...

  8. Uva 679 Dropping Ballls 二叉树的编号

    这个程序常规处理起来数据量很大,I可以高达2^D-1 /* ....... */ 里面的代码块据此避免了开太大的数组 做太多的循环 #include<cstdio> #include< ...

  9. 小游戏专场:腾讯云Game-Tech技术沙龙上海站顺利落下帷幕

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯游戏云发表于云+社区专栏 9月14日腾讯云GAME-TECH技术沙龙小游戏专场在上海顺利举办,此次技术沙龙由腾讯云的资深专家,以及 ...

随机推荐

  1. 收藏起来,史上最全的 MySQL 高性能优化实战总结!

    转自:https://mp.weixin.qq.com/s/sRsJzFO9dPtKhovJNWN3Dg 一.前言 MySQL 对于很多 Linux 从业者而言,是一个非常棘手的问题,多数情况都是因为 ...

  2. python 实现汉诺塔问题

    代码如下: def hano(n,x,y,z): if n==1: print(x,"->",z) else: #将n-1个盘子从x->y hano(n-1,x,z,y ...

  3. 浅谈RBF函数

    所谓径向基函数 (Radial Basis Function 简称 RBF), 就是某种沿径向对称的标量函数. 通常定义为空间中任一点x到某一中心xc之间欧氏距离的单调函数 , 可记作 k(||x-x ...

  4. Intellij IDEA错误识别.xml文件

    转自原文Intellij IDEA错误识别文件 今天上午弄了一个多小时,对idea感到十分的沮丧,真是太不好用了,一点儿都不智能,而且有些地方,还被自动的配置错误,导致操作起来就像是脱缰的野马. 言归 ...

  5. 日积月累--exception记录

    关于Android的sqlite数据类型text长度限制的问题? 这也许不能称为一个bug,但是比较坑,所以贴在了这里.在Android的sqlite中存储一个字符串,发现总是数据丢失,我去查询sql ...

  6. 微信php分享页面自定义标题与内容

    1.因为现在分享页面,发给朋友或者朋友圈都没办法自定义标题.图片和内容,所以必须要有微信公众号 2.如果有微信公众号可直接登录,如果没有要注册,注册完或者登录了 3.查看你的权限,左侧最下面开发的接口 ...

  7. ajax回调中window.open弹出的窗口会被浏览器拦截的解决方法

    存在问题:处理页面ajax请求过程中,异步请求成功后需要新开窗口打开 url,使用的是 window.open() 方法 来实现,最终都被浏览器拦截了.不会跳到对应的页面,如下 原因:浏览器之所以拦截 ...

  8. cpp面向对象编程

    如下图,先建好文件, 这里用的是Visual studio 2010 当然也可以用eclipse for cpp,如下图: AbstractShape.h #ifndef ABSTRACTSHAPE_ ...

  9. 2017.11.15 linux软件安装管理(todo)

    学习来自:http://www.imooc.com/learn/447 第一章 介绍 第二章 软件包简介 1.源码包 2.二进制包(RPM包或系统默认包) 脚本安装包其实是别人把软件安装的脚本写好了, ...

  10. Java: 获取当前执行位置的文件名/类名/方法名/行号

    在 JAVA 程序有时需要获取当前代码位置, 于是就利用 Thread.currentThread().getStackTrace() 写了下面这个工具类, 用来获取当前执行位置处代码的文件名/类名/ ...