poj2499 Binary Tree

先上题目:

9:Binary Tree

总时间限制: 
1000ms 
内存限制: 
65536kB
描述
Background
Binary trees are a common data structure in computer science. In this problem we will look at an infinite binary tree where the nodes contain a pair of integers. The tree is constructed like this:
  • The root contains the pair (1, 1).
  • If a node contains (a, b) then its left child contains (a + b, b) and its right child (a, a + b)

Problem
Given the contents (a, b) of some node of the binary tree described above, suppose you are walking from the root of the tree to the given node along the shortest possible path. Can you find out how often you have to go to a left child and how often to a right child?
输入
The first line contains the number of scenarios.
Every scenario consists of a single line containing two integers i and j (1 <= i, j <= 2*10 9) that represent
a node (i, j). You can assume that this is a valid node in the binary tree described above.
输出
The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing two numbers l and r separated by a single space, where l is how often you have to go left and r is how often you have to go right when traversing the tree from the root to the node given in the input. Print an empty line after every scenario.
样例输入
3
42 1
3 4
17 73
样例输出
Scenario #1:
41 0

Scenario #2:
2 1

Scenario #3:
4 6
来源
TUD Programming Contest 2005 (Training Session), Darmstadt, Germany

经分析,有以下四点:
1.给定一个pair,例如(left, right),那么在这棵树里,除根结点(1, 1)之外,其他节点的left!=right。

2.那么,(left, right)是如何得来的呢?因为left!=right,若left>right,则(left, right)一定是由(left-right, right)向左转得来的,若left<right,则(left, right)一定是由(left, right-left)向右转得来的。就这样倒推回(1, 1),分别统计向左转、向右转的次数即可。

从这里我们也可以看出,除(1, 1)、(1, right)和(left, 1)三种情况之外,left与right不可互相整除。

3.最坏情况下的时间复杂度是O(N),例如一直从左路向上回溯。

4.和二叉树关系不大。

上代码:
#include <iostream>
using namespace std;

int main()
{
	int n, i;
	int left, right;
	int l_turn, r_turn;

	freopen("D:\\in.txt", "r", stdin);
	freopen("D:\\out.txt", "w", stdout);

	cin>>n;
	for (i=0; i<n; ++i)
	{
		cin>>left;
		cin>>right;

		l_turn=0;
		r_turn=0;

		while (left!=1 || right!=1)
		{
			if(left>right)
			{
				left-=right;
				++l_turn;
			}
			else
			{
				right-=left;
				++r_turn;
			}
		}

		cout<<"Scenario #"<<i+1<<":"<<endl;
		cout<<l_turn<<' '<<r_turn<<endl;
		cout<<endl;
	}

	return 0;
}

提交上去,wtf?!,time limited exceeded,要知道最坏情况下的复杂度也不过是O(N)啊!再看题目,输入数据“ i and j (1 <= i, j <= 2*109”,上十亿规模了,如果测试数据中有(2000000000, 1),则要做几乎20亿次常数时间的操作,显然不可能在1000ms内完成,所以必须改进。

幸好改进的方法也足够简单:用除法代替减法。
#include <iostream>
using namespace std;

int main()
{
	int n, i;

	int left, right;
	int l_turn, r_turn;

	freopen("D:\\in.txt", "r", stdin);
	freopen("D:\\out.txt", "w", stdout);

	cin>>n;
	for (i=0; i<n; ++i)
	{
		cin>>left;
		cin>>right;

		l_turn=0;
		r_turn=0;

		while (left!=1 || right!=1)
		{
			if(left == 1)	//left是1,right肯定不是1;相当于一直从右路回溯  
			{
				r_turn += right-1;
				right = 1;
			}
			else if(right == 1)	//right是1,left肯定不是1;相当于一直从左路回溯
			{
				l_turn += left-1;
				left = 1;
			}
			else	//都不是1,则较大数除以较小数;且最终余数一定会是1
			{
				if(left>right)
				{
					l_turn += left/right;
					left %= right;
				}
				else
				{
					r_turn += right/left;
					right %= left;
				}
			}
		}

		cout<<"Scenario #"<<i+1<<":"<<endl;
		cout<<l_turn<<' '<<r_turn<<endl;
		cout<<endl;
	}

	return 0;
}

这样就ac了。





  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值