Educational Codeforces Round 18 -- D. Paths in a Complete Binary Tree(二叉树模拟)
来源:互联网 发布:淘宝女装店扒皮aviva 编辑:程序博客网 时间:2024/06/05 19:30
题意:
给你一棵完全二叉树, 要求从某个结点出发, 开始行走,可以向上走,向左走,向右走, 模拟这个过程。
思路:
观察那个二叉树的形状, 直接二分走就可以找到每一个结点。
我们先找到那个结点,然后从这个结点进行二分模拟行走, 因为有向上走的情况存在,所以可以开一个栈,来维护所走过的路。
#include <bits/stdc++.h>using namespace std;typedef long long LL;const int maxn = 1e5+10;struct Node{ LL v; LL L,R; Node(LL v = 0,LL L = 0,LL R = 0):v(v),L(L),R(R){}};stack<Node>sk;char s[maxn];int main(){ LL n; int q; scanf("%lld %d",&n, &q); while(q--){ LL m; scanf("%lld%s",&m,s); while(!sk.empty()) sk.pop(); LL l = 1, r = n; LL cur = l+r>>1LL; while(cur != m){ sk.push(Node(cur,l,r)); if (m < cur){ r = cur-1; } else l = cur + 1; cur = l + r >> 1LL; } for (int i = 0; s[i]; ++i){ if (s[i] == 'U'){ if (sk.empty()) continue; else { cur = sk.top().v; l = sk.top().L; r = sk.top().R; sk.pop(); } } else { if (l == r) continue; sk.push(Node(cur,l,r)); if (s[i] == 'L'){ r = cur-1; } else l = cur+1; cur = l + r >> 1LL; } } printf("%lld\n",cur); } return 0;}
0 0
- Educational Codeforces Round 18 -- D. Paths in a Complete Binary Tree(二叉树模拟)
- Educational Codeforces Round 18 D. Paths in a Complete Binary Tree
- D. Paths in a Complete Binary Tree CodeForces
- 【CodeForces】792D Paths in a Complete Binary Tree
- Codeforces 792D Paths in a Complete Binary Tree 规律+模拟
- Paths in a Complete Binary Tree codeforces
- CF792D:Paths in a Complete Binary Tree(数学 & 规律)
- Educational Codeforces Round 10(A)模拟
- Codeforces Educational Round #18(Codeforces 792 A B C D)
- Codeforces-Educational Codeforces Round 32-(A,B,C,D)
- Educational Codeforces Round 25 A. Binary Protocol
- Educational Codeforces Round 25 A. Binary Protocol
- CF Educational Codeforces Round 15(A~D)
- Educational Codeforces Round 2 A. Extract Numbers(模拟)
- Educational Codeforces Round 1 (A)模拟
- Educational Codeforces Round 5(A) 模拟
- Educational Codeforces Round 21 D ( 二分 )
- Educational Codeforces Round 6 (B)模拟
- React Native ViewPagerAndroid使用小结
- PAT B1061
- NYOJ 203 三国志 最短路 + 贪心 01 背包 的思想
- [hdu4656] Evaluation
- 机器学习常用评价指标总结
- Educational Codeforces Round 18 -- D. Paths in a Complete Binary Tree(二叉树模拟)
- 没时间解释了,快上车!Angular入门第三讲:$apply()的使用
- XTU2016CCPC中南邀请赛C
- PAT B1020
- Using API with Catia
- BZOJ 2822: [AHOI2012]树屋阶梯
- PAT B1023
- 一个很好隐藏Handler方法的url解析器(映射器)通常和多url请求方法的Handler类连用
- wince6.0 cvrtbin将*.bin转到*.nb0