山东省第五届省赛回顾 Full Binary Tree
2015-04-25 18:37
417 查看
Full Binary Tree
题目链接:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2882Time 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 willbe 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.示例输入
5 1 2 2 3 4 3 1024 2048 3214567 9998877
示例输出
1 2 3 1 44
提示
来源
2014年山东省第五届ACM大学生程序设计竞赛解题思路:
题意就是给定一个满二叉树,根节点的数为1,左边的子节点为父节点的2倍,右边子节点为父节点2倍加1;
1
2 3 ☜如图
4 5 6 7 可以看出每一层的数x满足 2^p <=x<2^(p+1) (p代表层数)
8 9 10 11 12 13 14 15
所求的就是给定二叉树中的两个数,求其最短距离,也就是求最近公共父节点
#include <iostream> #include <algorithm> using namespace std; const int maxn=99999; int f[maxn]; int len; void init()//初始化,画出满二叉树 { f[0]=1; for(int i=1;;i++) { f[i]=f[i-1]*2; if(f[i]>1e9) { len=i-1; break; } } } int main() { init(); //cout<<"len"<<len<<endl; int t; cin>>t; while(t--) { int a,b; int step=0; cin>>a>>b; if(a>b) swap(a,b);//用b代表较大的数 int pos; //pos代表较小的数所在层 for(int i=0;i<len;i++) { if(a>=f[i]&&a<f[i+1])//求出较小的数所在的层数 { pos=i; break; } } while(1) { if(b>=f[pos]&&b<f[pos+1]) //较大的数往上跳,直到与较小的数在同一层 break; else { b/=2; step++; } } while(a!=b) { b/=2; //然后两个数一起往上跳 a/=2; step+=2; } cout<<step<<endl; } return 0; }
相关文章推荐
- 山东省第五届省赛回顾 Full Binary Tree
- 2014山东省第五届ACM省赛 Full Binary Tree
- 山东省第五届省赛 F - Full Binary Tree (利用满二叉树性质的一个思维题)
- 山东省第五届ACM大学生程序设计竞赛-Full Binary Tree(二叉树&&求任意两节点路径)
- 2014年山东省第五届ACM--Full Binary Tree (满二叉树)
- 山东省第五届ACM大学生程序设计竞赛 Full Binary Tree
- 山东省第五届ACM大赛--Problem E: Full Binary Tree
- Full Binary Tree(山东省第五届ACM大学生程序设计竞赛 )
- 山东省第五届ACM省赛题——Full Binary Tree(二叉树应用)
- 第五届山东省ACM Full Binary Tree
- 第五届ACM大学生程序设计竞赛:Full Binary Tree
- 山东省第五届ACM省赛 Colorful Cupcakes(记忆化搜索)
- Full Binary Tree(有关二叉树)
- 2014山东省第五届ACM省赛 Hearthstone II
- Construct Full Binary Tree from given preorder and postorder traversals
- [ACM] sdut 2882 Full Binary Tree (满二叉树的公共祖先)
- hihocoder 1342 Full Binary Tree Picture【完全二叉树】
- Data Structure Binary Tree: Construct Full Binary Tree from given preorder and postorder traversals
- full binary tree != 满二叉树
- 山东省第五届省赛 A题 angry_birds_again_and_again(简单数学 积分问题)