题目链接

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. 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

1.刚开始模拟了一下两个深度分别为3和4的样例,然后就发现在第i层中,首先落下的2i-2次都是偶数且这2i-2中按奇偶数来分正好是把左一半右一半的偶数叶子节点逐一遍历,同理接下来的2i-2次都是奇数。然后直接写了一个规律的模拟,过了样例,交——WA。又读了一遍题,发现小球数目是任意的,而我刚开始按最多2i-1次个小球来考虑了

2.读了几遍标答,手动模拟,搞懂了。以前做过开关灯的问题,和这个确实有思想上的类似的,可以对比着想

3.想去看下大神们有没有什么更好的理解,确实有。某博客:第i个球走的路径是i-1二进制数的逆序。0代表左子树,1代表右子树。并由右向左走D-1次即可

4.大概想了一下上面的方法,果然是很神奇的规律,我们发现从0开始递增的数,对应二进制从右向左看,第一位的0、1交替周期是2,第二位的周期为4,第三位的周期为8…而且每一位都是从0开始,刚好和二叉树的每一层开关的交替周期相同!故在这个题中将0代表左子树,1代表右子树的话,刚好是依次取D-1的每一位,直到判断到最终的叶子节点上

标答:

#include <iostream>
using namespace std;
int main(){int T,d,I;while(scanf("%d",&T)!=EOF){if(T==-1) break;while(T--){scanf("%d%d",&d,&I);int ans=1;for(int i=0;i<d-1;i++){  ///写位运算可大大节省时间if(I&1){ans<<=1;I=(I+1)>>1;}else{ans=(ans<<1)+1;I>>=1;}}printf("%d\n",ans);}}return 0;
}

神奇的规律:

#include <cstdio>
#include <iostream>
using namespace std;
int main(){int T,p,q;while(scanf("%d",&T)!=EOF){if(T==-1) break;while(T--){scanf("%d%d",&p,&q);int x=q-1;int ans=1;while(--p){if(x&1){    ///判断最后一位是否为1,也可当做奇数偶数的判断ans=(ans<<1)+1;}else ans<<=1;x>>=1;}printf("%d\n",ans);}}return 0;
}

UVa679 Dropping Balls (满二叉树+开关灯思想)相关推荐

  1. UVa-679 Dropping Balls(二叉树的编号)

    题目描述如下: 有一棵二叉树,最大深度为D,且所有叶子的深度都相同.所有结点从上到下从左到右 编号为1, 2, 3,-, 2D-1.在结点1处放一个小球,它会往下落.每个内结点上都有一个开关, 初始全 ...

  2. 679 - Dropping Balls

    Dropping Balls PS:因为该题排版较麻烦,这里给出OJ网址:UVa679 - Dropping Balls 有一棵二叉树,最大深度为D,且所有叶子的深度都相同.所有结点从上到下从左到右编 ...

  3. UVA679 小球下落 Dropping Balls(二叉树的编号)

    许多的小球一个一个的从一棵满二叉树上掉下来组成一个新满二叉树,每一时间,一个正在下降的球第一个访问的是非叶子节点.然后继续下降时,或者走右子树,或者走左子树,直到访问到叶子节点. 决定球运动方向的是每 ...

  4. 例题6-6 小球下落(Dropping Balls, UVa 679)

    例题6-6 小球下落(Dropping Balls, UVa 679) 许多的小球一个一个的从一棵满二叉树上掉下来组成一个新满二叉树,每一时间,一个正在下降的球第一个访问的是非叶子节点.然后继续下降时 ...

  5. step3 . day6数据结构之非线性表 满二叉树和不完全二叉树

    二叉树和链表相似,只是后节点变成了左右节点,重要的是递归思想的理解和返回时候的层级结构 1.满二叉树的穿件及前中后序遍历 #include <stdio.h> #include <s ...

  6. 【algods】4.树和二叉树、完全二叉树、满二叉树、二叉查找树、平衡二叉树、堆、哈夫曼树、散列表...

    本博客内容耗时4天整理,如果需要转载,请注明出处,谢谢. 1.树 1.1树的定义 在计算机科学中,树(英语:tree)是一种抽象数据类型(ADT)或是实作这种抽象数据类型的数据结构,用来模拟具有树状结 ...

  7. 力扣——所有可能的满二叉树

    满二叉树是一类二叉树,其中每个结点恰好有 0 或 2 个子结点. 返回包含 N 个结点的所有可能满二叉树的列表. 答案的每个元素都是一个可能树的根结点. 答案中每个树的每个结点都必须有 node.va ...

  8. 利用二叉树的思想来实现分配和释放内存方法

    虽然大部分系统都有提供内存动态分配和释放函数(即C语言中的malloc和free函数),但是在嵌入式开发中由于系统的限制往往需要自己来实现内存管理,如在有些平台上可动态申请的最大空间不能满足程序设计的 ...

  9. E2. Rubik‘s Cube Coloring (hard version) dp,满二叉树(2300)

    题意 : 其他条件和上题相同 这里已知n个点,给定每个点的编号和颜色 求这个树的合法染色方案,取模1e9 + 7 思路 : 首先预处理d[c][k]d[c][k]d[c][k]表示根结点颜色为c,深度 ...

最新文章

  1. mysql conflicts with_安装MYSQL错误“conflicts with file from package mysql-libs-*” 解决方法...
  2. QT的QMutableVectorIterator类的使用
  3. 需求获取安排计划书_6分钟教你写一份融资计划书
  4. STM32——ADC
  5. 如何使用Nginx服务开启HTTP2
  6. 蓝桥杯 基础练习 2n皇后问题(从n皇后问题入手)
  7. Hive Cli 和 Beeline
  8. VMware Cloud Director 10.3.1 - 云计算调配和管理平台
  9. Windows C++界面库
  10. PHP生成二维码方法
  11. python读取rtf文件_在python中将unicode文本输出到RTF文件
  12. 宽带波形测试软件,适用于5G时代的波形测试分析系统是怎样的?
  13. marked转换html失败,JavaScript使用marked.js在线Markdown转HTML
  14. 卧底“刷量”卖家,有关微信公众号“刷量”的五个劲爆事实
  15. python opcua使用教程
  16. linux如何初始化根文件系统,搭建自己的Linux根文件系统
  17. word 更新全局域
  18. DS18B20温度转换与显示
  19. 软件架构设计之如何编排复杂多任务
  20. MyBatis 开发有bug找不到?多看看执行流程

热门文章

  1. 微信公众号会替代手机APP吗?
  2. 满足条件的整数 - 详解
  3. 最佳治理实践?一文读懂YFI运行机制
  4. 基于Nodejs服务器下,python搭配ffmpeg实现推流直播
  5. MUR60120PT-ASEMI整流二极管MUR60120PT
  6. Perl常用模块使用例子
  7. 如何设计一款高性能分布式锁,实现数据的安全访问?
  8. 人工智能---我们的骄傲
  9. 【手绘漫画】图解LeetCode之相交链表(LeetCode 160)
  10. Vue全家桶之webpack详解(四)