Codeforces Round #287 (Div. 2) C 构造
来源:互联网 发布:java获取泛型t的class 编辑:程序博客网 时间:2024/06/04 19:26
链接:戳这里
C. Guess Your Way Out!
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Amr bought a new video game "Guess Your Way Out!". The goal of the game is to find an exit from the maze that looks like a perfect binary tree of height h. The player is initially standing at the root of the tree and the exit from the tree is located at some leaf node.Let's index all the leaf nodes from the left to the right from 1 to 2h. The exit is located at some node n where 1 ≤ n ≤ 2h, the player doesn't know where the exit is so he has to guess his way out!
Amr follows simple algorithm to choose the path. Let's consider infinite command string "LRLRLRLRL..." (consisting of alternating characters 'L' and 'R'). Amr sequentially executes the characters of the string using following rules:
Character 'L' means "go to the left child of the current node";
Character 'R' means "go to the right child of the current node";
If the destination node is already visited, Amr skips current command, otherwise he moves to the destination node;
If Amr skipped two consecutive commands, he goes back to the parent of the current node before executing next command;
If he reached a leaf node that is not the exit, he returns to the parent of the current node;
If he reaches an exit, the game is finished.
Now Amr wonders, if he follows this algorithm, how many nodes he is going to visit before reaching the exit?
Input
Input consists of two integers h, n (1 ≤ h ≤ 50, 1 ≤ n ≤ 2h).
Output
Output a single integer representing the number of nodes (excluding the exit node) Amr is going to visit before reaching the exit by following this algorithm.
Examples
input
1 2
output
2
input
2 3
output
5
input
3 6
output
10
input
10 1024
output
2046
Note
A perfect binary tree of height h is a binary tree consisting of h + 1 levels. Level 0 consists of a single node called root, level h consists of 2h nodes called leaves. Each node that is not a leaf has exactly two children, left and right one.
Following picture illustrates the sample test number 3. Nodes are labeled according to the order of visit.
题意:
深度为h的二叉树,现在要从节点1通过LRLRLRLRLR....这样的路径走到标号为(2^h+n-1)的叶子节点
这里其实就是最后一层叶子结点从左往右数第n个,需要多少步。走过节点跳过
思路:
暴力去遍历这样的路径肯定会T
那么考虑可不可以少计算一点,因为如果n在当前结点的左边但是却往右走,可以直接加上右边的节点个数转走左边
同理,如果n在当前结点的右边但是却往左走。可以直接加上左边的节点个数转走右边
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<iomanip>#include<cmath>#include<bitset>#define mst(ss,b) memset((ss),(b),sizeof(ss))///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef long double lb;#define INF (1ll<<60)-1#define Max 1e9using namespace std;int h;ll n;ll a[55];int main(){ a[0]=1; for(int i=1;i<=50;i++) a[i]=a[i-1]*2LL; scanf("%d%I64d",&h,&n); ll ans=1; int f=0; /// L->0 R->1 while(n && h){ if(f==0){ if(n-a[h-1]>0) { ans+=a[h]; n-=a[h-1]; h--; f=0; } else { h--; f=1; ans++; } } else { if(n-a[h-1]>0){ n-=a[h-1]; h--; f=0; ans++; } else { ans+=a[h]; f=1; h--; } } ///printf("%I64d %I64d\n",n,ans); } printf("%I64d\n",ans-1); return 0;}
0 0
- Codeforces Round #287 (Div. 2) C. (构造)
- Codeforces Round #287 (Div. 2) C 构造
- Codeforces Round #105 (Div. 2) C 构造
- Codeforces Round #372 (Div. 2) C 构造
- Codeforces Round #235 (Div. 2) C Team 构造法
- Codeforces Round #217 (Div. 2) / 370C Mittens (构造&贪心)
- 构造数列Codeforces Round #261 (Div. 2)C
- Codeforces Round #268 (Div. 2) C 24 Game [构造]
- Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)
- Codeforces Round #275 (Div. 2) C. Diverse Permutation 构造
- Codeforces Round #283 (Div. 2) C. Removing Columns 模拟+构造
- Codeforces Round #285 (Div. 2) C. Misha and Forest 构造
- *Codeforces Round #337 (Div. 2) C Harmony Analysis 构造题
- Codeforces Round #342 (Div. 2)(C)构造
- Codeforces Round #337 (Div. 2) C. Harmony Analysis (构造)
- Codeforces Round #337 (Div. 2) C 找规律+构造
- Codeforces Round #381 (Div. 2)C(构造,思维)
- Codeforces Round #224 (Div. 2) C. Arithmetic Progression【构造等差数列】
- Light oj 1094 Farthest Nodes in a Tree(树的最大直径)
- vs2010 LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- Tempter of the Bone
- linux下文件操作需要关注的函数
- nginx 配置多域名
- Codeforces Round #287 (Div. 2) C 构造
- java基础----day02:运算符
- cocoa touch——UIImage——create,init
- 腾讯优测优分享干货精选| Android双卡双待适配——隐藏在数据库中的那些秘密
- WebView 与js交互基础总结
- UINib
- linuxfl2440开发板驱动开发之按键驱动
- 二分最大匹配(匈牙利算法+HK算法)
- Error creating bean with name 'menuController': Injection of autowired dependenc