hdu5444 (2015 ACM/ICPC Asia Regional Changchun Online)
2016-08-12 18:23
253 查看
题面
Elves are very peculiar creatures. As we all know, they can live for a very long time and their magical prowess are not something to be taken lightly. Also, they live on trees. However, there is something about them you may not know. Although delivering stuffs through magical teleportation is extremely convenient (much like emails). They still sometimes prefer other more “traditional” methods.So, as a elven postman, it is crucial to understand how to deliver the mail to the correct room of the tree. The elven tree always branches into no more than two paths upon intersection, either in the east direction or the west. It coincidentally looks awfully like a binary tree we human computer scientist know. Not only that, when numbering the rooms, they always number the room number from the east-most position to the west. For rooms in the east are usually more preferable and more expensive due to they having the privilege to see the sunrise, which matters a lot in elven culture.
Anyways, the elves usually wrote down all the rooms in a sequence at the root of the tree so that the postman may know how to deliver the mail. The sequence is written as follows, it will go straight to visit the east-most room and write down every room it encountered along the way. After the first room is reached, it will then go to the next unvisited east-most room, writing down every unvisited room on the way as well until all rooms are visited.
Your task is to determine how to reach a certain room given the sequence written on the root.
For instance, the sequence 2, 1, 4, 3 would be written on the root of the following tree.
题意
一个树中序遍历是1…n然后给你一个先序遍历序列,对于每次询问x,问你走到位置x的行动序列解法
先序遍历的特点是左子树右子树在后面两段连续的区间里,所以只要每次二分一下中间位置,然后递归求解子问题就好了。代码
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int SIZE = 1005; int a[SIZE]; void solve(int tg, int now, int r) { if(tg == a[now]) return; int wst = upper_bound(a + now, a + r, a[now]) - a; if(tg > a[now]) { printf("W"); solve(tg, wst, r); } else { printf("E"); solve(tg, now + 1, wst); } } int main() { int T; scanf("%d",&T); while(T--) { int n; scanf("%d",&n); for(int i = 0; i < n; i++) scanf("%d",&a[i]); int q; scanf("%d",&q); while(q--) { int t; scanf("%d",&t); solve(t, 0, n); puts(""); } } }
相关文章推荐
- 【区间查询&遍历建树】2015 ACMICPC Asia Regional Changchun Online 1007&1008
- HDU 5438.Ponds【2015 ACM/ICPC Asia Regional Changchun Online】【DFS】9月13
- hdu 5437 Alisha’s Party 优先队列 2015 ACM/ICPC Asia Regional Changchun Online
- HDU 5446 Unknown Treasure(lucas定理+中国剩余定理)——2015 ACM/ICPC Asia Regional Changchun Online
- hdu 5443 The Water Problem 水题 2015 ACM/ICPC Asia Regional Changchun Online
- 2015 ACM/ICPC Asia Regional Changchun Online(1008)
- HDU 5446 Unknown Treasure(中国剩余定理+卢卡斯定理)——2015 ACM/ICPC Asia Regional Changchun Online
- 【优先队列】HDU5437Alisha’s Party【2015 ACM/ICPC Asia Regional Changchun Online】
- 2015 ACMICPC Asia Regional Changchun Online
- hdu 5444 Elven Postman 树的先序遍历 2015 ACM/ICPC Asia Regional Changchun Online
- HDU 5438 Ponds (拓扑排序+DFS)2015 ACM/ICPC Asia Regional Changchun Online
- HDU 5441 Travel(2015 ACM/ICPC Asia Regional Changchun Online)
- 2015 ACM/ICPC Asia Regional Changchun Online Pro 1002 Ponds(拓扑排序+并查集)
- 2015 ACM/ICPC Asia Regional Changchun Online(hdu 5437 - hdu 5449)
- HDOJ-5446/2015 ACM/ICPC Asia Regional Changchun Online 1010(数论)
- HDU 5438 Ponds(dfs)——2015 ACM/ICPC Asia Regional Changchun Online
- HDU 5443 The Water Problem 2015 ACM/ICPC Asia Regional Changchun Online
- 2015 ACM/ICPC Asia Regional Changchun Online
- 2015 ACM/ICPC Asia Regional Changchun Online Pro 1005 Travel (Krsukal变形)
- hdu5443(2015 ACM/ICPC Asia Regional Changchun Online )