2014山东省第五届ACM省赛 Full Binary Tree
来源:互联网 发布:2015云计算市场规模 编辑:程序博客网 时间:2024/05/19 16:35
Full Binary Tree
Time Limit: 2000MS Memory limit: 65536K
题目描述
In computer science, a binary tree is a tree data structure in which each node has at most two children. Consider an infinite full binary tree (each node has two children except the leaf nodes) defined as follows. For a node labelled v its left child will be labelled 2 * v and its right child will be labelled 2 * v + 1. The root is labelled as 1.
You are given n queries of the form i, j. For each query, you have to print the length of the shortest path between node labelled i and node labelled j.
输入
First line contains n(1 ≤ n ≤ 10^5), the number of queries. Each query consists of two space separated integers i and j(1 ≤ i, j ≤ 10^9) in one line.
输出
For each query, print the required answer in one line.
示例输入
51 22 34 31024 20483214567 9998877
示例输出
123144
提示
来源
2014年山东省第五届ACM大学生程序设计竞赛
题意:给二叉树的两个结点标号i和j(标号从1开始),求它们间路径的长度。
思路:当i!=j时直接将大的一个除以2,不断循环,每次答案+1。
初始时两点深度可能不等,将深度大的不断减小,当两者深度相等时,若i==j则可输出答案。否则继续循环下去,它们的深度差为0或1,总会在某个时候i==j。
#include <iostream> using namespace std; int main() { int T,ans; cin>>T; while(T--) { int a,b,t; ans=0; cin>>a>>b; t=a; if(a<b) { a=b; b=t; } while(a!=b) { a/=2; ans++; t=a; if(a<b) { a=b; b=t; } } cout<<ans<<endl; } return 0; }
0 0
- 2014山东省第五届ACM省赛 Full Binary Tree
- 2014山东省第五届ACM省赛 Full Binary Tree
- 第五届山东省ACM Full Binary Tree
- 山东省第五届ACM大学生程序设计竞赛 Full Binary Tree
- 山东省第五届ACM大赛--Problem E: Full Binary Tree
- 2014年山东省第五届ACM--Full Binary Tree (满二叉树)
- 山东省第五届ACM省赛题——Full Binary Tree(二叉树应用)
- Full Binary Tree(山东省第五届ACM大学生程序设计竞赛 )
- 山东省第五届省赛回顾 Full Binary Tree
- 山东省第五届ACM大学生程序设计竞赛-Full Binary Tree(二叉树&&求任意两节点路径)
- 第五届ACM大学生程序设计竞赛:Full Binary Tree
- Full Binary Tree 2014 SD第五届ACM大学生程序设计竞赛 F
- 2014山东省第五届ACM省赛
- 2014山东省第五届ACM省赛 angry_birds_again_and_again
- 2014山东省第五届ACM省赛 Factorial
- 2014山东省第五届ACM省赛 Circle
- 2014山东省第五届ACM省赛 angry_birds_again_and_again
- 2014山东省第五届ACM省赛 Circle
- android 根据时间是否过期进行文件的删除
- ios swfit 自定义柱状图
- healthkit框架使用
- sqlite3_get_table()
- 面试题之实现3^100次方和大整数的相乘
- 2014山东省第五届ACM省赛 Full Binary Tree
- iOS网络编程TCP/IP应用篇(三)-协议的定制
- Crosswalk web引擎Android中的使用
- Mac垃圾清理工具使用注意点
- Collection接口
- Activity的四种启动模式--看Android开发艺术探索总结
- C++ BSON 帮助函数
- Keepalived+lvs+nginx之负载均衡
- Android高效加载大图、多图解决方案,有效避免程序OOM