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)。
代码如下:
#include <iostream>
using namespace std; int main()
{
int n; scanf( "%d", &n );
for ( int i = ; i < n; ++i )
{
int a, b;
int l, r; scanf("%d %d", &a, &b );
l = r = ;
while( a != || b!= )
{
if ( a == )
{
r += b - a;
b = ;
}
else
if ( b == )
{
l += a - ;
a = ;
}
else
if ( a > b )
{
l += a / b;
a -= b * ( a / b );
}
else
if ( a < b )
{
r += b / a;
b -= a *( b / a );
}
} printf( "Scenario #%d:\n%d %d\n\n", i + , l, r );
} return ;
}
Poj 2499 Binary Tree(贪心)的更多相关文章
- POJ 2499 Binary Tree
题意:二叉树的根节点为(1,1),对每个结点(a,b)其左结点为 (a + b, b) ,其右结点为 (a, a + b),已知某结点坐标,求根节点到该节点向左和向右走的次数. 分析:往回一步一步走肯 ...
- POJ 2499 Binary Tree(二叉树,找规律)
题意:给一个这样的二叉树,每个节点用一对数(a,b)表示,根节点为(1,1).设父亲为(a,b),左儿子(a+b,b),右儿子(a,a+b). 给几组数据,(i,j),求从根节点到(i,j)节点需要向 ...
- Binary Tree 分类: POJ 2015-06-12 20:34 17人阅读 评论(0) 收藏
Binary Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6355 Accepted: 2922 Descr ...
- 九章算法系列(#3 Binary Tree & Divide Conquer)-课堂笔记
前言 第一天的算法都还没有缓过来,直接就进入了第二天的算法学习.前一天一直在整理Binary Search的笔记,也没有提前预习一下,好在Binary Tree算是自己最熟的地方了吧(LeetCode ...
- 2015上海赛区B Binary Tree
B - Binary Tree Description The Old Frog King lives on the root of an infinite tree. According to ...
- 863. All Nodes Distance K in Binary Tree 到制定节点距离为k的节点
[抄题]: We are given a binary tree (with root node root), a target node, and an integer value K. Retur ...
- 536. Construct Binary Tree from String 从括号字符串中构建二叉树
[抄题]: You need to construct a binary tree from a string consisting of parenthesis and integers. The ...
- 742. Closest Leaf in a Binary Tree查找最近的叶子节点
[抄题]: Given a binary tree where every node has a unique value, and a target key k, find the value of ...
- 106. Construct Binary Tree from Inorder and Postorder Traversal根据后中序数组恢复出原来的树
[抄题]: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assum ...
随机推荐
- Javascript初级学习总结
首先,在Html页面输出,使用document.write('内容'); <html> <head> <title></title> <scrip ...
- 关于js封装框架类库之DOM操作模块(二)
上一篇基本实现了框架结构,但是与真正能用上的项目框架比较还是存在很多不足,在这又做了些加强与优化 (function ( window, undefined ) { var arr = [], pus ...
- Windows Server 2012 R2 服务器管理器介绍和配置使用
1. 服务管理器是用于管理系统服务的管理工具.一般常用于windows系统,使用这个工具你可以启动.停止服务:设置服务是自动.手动启动或禁用:查看某个服务的相关信息:设置服务以什么用户启动等等(一般包 ...
- zoj 3490
蛋都疼了,高了半天,Output Limit Exceeded 原来是输入的问题,我靠!!以后还是用输入输出c++好,这尼玛!!郁闷!!!!! #include<stdio.h> #inc ...
- iOS 从app跳转到Safari、从app打开电话呼叫
1.从app跳转到Safari NSString* strIdentifier = @"http://www.ybyb.com"; BOOL isExsit = [[UIAppli ...
- Undefined symbols for architecture xxx
解决方法: "Build Settings"->"Linking"->"Other Linker Flags" add the ...
- ##DAY4 事件的基本概念、触摸的基本概念、响应者链、手势
##DAY4 事件的基本概念.触摸的基本概念.响应者链.手势 #pragma mark ———————事件的基本概念 ——————————— 事件的基本概念: 1)事件是当用户的手指触击屏幕及在屏幕 ...
- The Painter's Partition Problem Part II
(http://leetcode.com/2011/04/the-painters-partition-problem-part-ii.html) This is Part II of the art ...
- [Swust OJ 589]--吃西瓜(三维矩阵压缩)
题目链接:http://acm.swust.edu.cn/problem/589/ Time limit(ms): 2000 Memory limit(kb): 65535 Description ...
- 图片剪切之Croppic插件
前几天做图片上传时需要进行图片的剪切和缩放,上网查找时找到了这个插件.样式很好看,功能也很OK.但是网上都是php进行后台处理图片的例子,然后只好慢慢琢磨C#的处理.插件地址是:http://www. ...