Codeforces Round #287 (Div. 2) C. Guess Your Way Out! 二叉树遍历
2015-07-31 14:25
411 查看
思路:由样例可知,若exit和实际command在当前的父节点两边,则父节点一侧的子树节点必须全部遍历一遍,才会再次返回到父节点,从而转向正确的方向。
故先求出exit的正确路径,然后与command相比,
①若相同,则只需沿着command走,ans++
②若不相同,则ans += 父节点左侧子树的节点数
代码如下:
故先求出exit的正确路径,然后与command相比,
①若相同,则只需沿着command走,ans++
②若不相同,则ans += 父节点左侧子树的节点数
代码如下:
#include <cstdio> using namespace std; #define N 51 #define LEFT 0 #define RIGHT 1 long long fac , sum ; int dir ; int main(){ int h; long long n; scanf("%d %I64d", &h, &n); fac[0] = 1, sum[0] = 1; for(int i = 1; i <= h; ++i) fac[i] = fac[i - 1] * 2, sum[i] = sum[i - 1] + fac[i]; int level = h; long long ans = 0, cur = 0; int cmd = LEFT; for(int i = h; i > 0; --i){ if(n > cur + fac[i - 1]) dir[i] = RIGHT, cur += fac[i - 1]; } while(level){ if(cmd == dir[level]){ ++ans, --level; } else ans += sum[level - 1]; cmd = 1 - cmd; } printf("%I64d\n", ans); return 0; }
相关文章推荐
- request.getScheme()的使用方法
- iOS UItableView通讯录
- easyUI验证validatebox
- 关于Android Stduio插件的问题
- UVA 1152 4 Values Whose Sum is Zero 和为0的4个值 (中途相遇)
- UI02_UIButton
- BlueDBM个人读感
- iOS-UI-07 代理的使用
- c# request了解一下
- HDOJ丶 1005(Number Sequence)
- error MSB8031 Building an MFC project for a non-Unicode character set is deprecated
- QT下ui类的this指针与ui指针
- guava (四)--集合中的FluentIterable类
- [XCode] XCode 6 中 UIPickerView 的简单示例
- 【iOS开发系列】物理仿真动画UIDynamic (1)
- hdu3450Counting Sequences 树状数组
- mysql 连接url中useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true 的作用
- easyui在java-itoo的管理流程
- Clique in the Divisibility Graph
- HTML 对象属性 property value 与 attribute value