山东省第五届省赛回顾 Full Binary Tree

Full Binary Tree

题目链接:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2882

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.
 

示例输入

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;
}



posted @ 2015-04-25 18:37  编程菌  阅读(174)  评论(0编辑  收藏  举报