poj 2499

题目链接:点击打开链接

二叉树的应用,一开始自己真的没有思路,参考了http://blog.csdn.net/yangliuy/article/details/7250997

自己一开始真的TLE,还真的是,关键照着作者又降到了0ms,厉害!

最近荒废了很多时间,不,忙活别的事情了,不要忧虑未来,不要忧虑自己参加acm有没有结果,因为,这对于结果毫无影响,在忧虑与悔恨的相关联的感情中,是对人产生影响最大的两种负能量,吸取教训!

对于代码中的优化,我们可以模拟出来,以图片中的结点对应代码:

/*************************************************************************
     File Name: 2499.cpp
     Author: yubo
     Mail: [email protected]
     Created Time: 2014年05月14日 星期三 04时35分04秒
     学习重点:
 ************************************************************************/

#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;
//貌似这种行为就可以将变化的参数按值返回
void TraveCount(int &l,int &r,int a,int b)
{
/*	if(a==1 && b==1){
		return ;
	}
	if(a>b)
		TraveCount(++l,r,a-b,b);
	else TraveCount(l,++r,a,b-a);*/
	while(a!=1 || b!=1){
		if(a==1){//若仅a为1,则只走右路,该点必然是右孩子
			r+=b-a;
			break;
		}
		if(b==1){
			l+=a-b;
			break;
		}
		if(a>b){//利用a,b的倍数关系,加快推进速度
			l+=a/b;
			a-=b*(a/b);
		}
		else
		{
			r+=b/a;
			b-=a*(b/a);
		}
	}

}
int main()
{
//	freopen("in.txt","r",stdin);
	int n,a,b,l,r,i=1;
	scanf("%d",&n);
	while(n--){
	scanf("%d%d",&a,&b);
		l=r=0;
		TraveCount(l,r,a,b);
		printf("Scenario #%d:\n",i++);
		printf("%d %d\n\n",l,r);
	}
}

poj 2499

时间: 2024-09-15 14:08:57

poj 2499的相关文章

POJ 2499 Binary Tree 题解

本题使用所谓的辗转相除法. 还需要逆过来遍历二叉树.可以想象给出的数据点是根节点,然后遍历到根节点(1,1). 考的是根据给出的规则,总结规律的能力. #include <stdio.h> namespace BinaryTree2499_1 { int main() { int T, a, b, le, ri; scanf("%d", &T); for (int t = 1; t <= T; t++) { scanf("%d %d", &

Poj 2499 Binary Tree

题目链接:http://poj.org/problem?id=2499 思路: 结点向左边移动时结点(a, b)变为( a+b, b),向右边移动时( a, b )变为( a, a + b); 为求最短路径<a1, a2, a3,...,an>,考虑从已经知道的结点(a, b)开始找出最短路径回到根节点(1, 1): 即向左移动次数和向右移动次数最少回到根节点,由贪心算法,若 a>b 时,a 减少最大即减去 b: 若 a < b,b 减少最大即减去a值,循环直到到达根节点(1, 1

POJ 2499 A*求第K短路

DES就是给你一个图.然后给你起点和终点.问你从起点到终点的第K短路. 第一次接触A*算法. // 大概懂思路了.A*算法需要的估价函数里的两个函数.一个是起点到当前点的消耗. //一个是当前点到目标点的估测消耗.所以需要用Dijstra或者Spfa求出目标点到所有点的最短路. //然后就可以用A*算法来求了. // 确实.学了位运算.链式表. #include<cstdio> #include<iostream> #include<queue> #include<

POJ 2499 Binary Tree 数学题解

Description 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,

POJ刷题(2499)

程序(已经Accepted): #include <stdio.h> /* * 逆向思维,从(m,n)到(1,1), * 给定(m,n),求其父亲,若m>n,则其父亲为(m-n,n),否则为(m,n-m), * 但是这样做会TLE,这就需要用除法代替减法,加快速度, * 也就是辗转相除法 * */ int main(void) { int n, a, b, l, r, temp, i; scanf("%d", &n); for(i=0; i<n; i++

POJ训练计划

POJ训练计划 Step1-500题 UVaOJ+算法竞赛入门经典+挑战编程+USACO 请见:http://acm.sdut.edu.cn/bbs/read.php?tid=5321 一.POJ训练计划 Moon修订 298道题集训第一天 POJ纯水题 = =: 17道题 2017 1218 2000 1046 1218 1003 1004 1005 1008 1013(枚举) 1207 1552 2105 2388 1316 2499 3006(筛法求素数) 正式集训计划: 未做  已做 

POJ - 3186 Treats for the Cows (区间DP)

题目链接:http://poj.org/problem?id=3186 题意:给定一组序列,取n次,每次可以取序列最前面的数或最后面的数,第n次出来就乘n,然后求和的最大值. 题解:用dp[i][j]表示i~j区间和的最大值,然后根据这个状态可以从删前和删后转移过来,推出状态转移方程: dp[i][j]=max(dp[i+1][j]+value[i]*k,dp[i][j-1]+value[j]*k) 1 #include <iostream> 2 #include <algorithm&

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

POJ——T2271 Guardian of Decency

http://poj.org/problem?id=2771 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5932   Accepted: 2463 Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is