Elven Postman

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 147    Accepted Submission(s): 90

Problem Description
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.

 
Input
First you are given an integer T(T≤10) indicating the number of test cases.

For each test case, there is a number n(n≤1000) on a line representing the number of rooms in this tree. n integers representing the sequence written at the root follow, respectively a1,...,an where a1,...,an∈{1,...,n}.

On the next line, there is a number q representing the number of mails to be sent. After that, there will be q integers x1,...,xq indicating the destination room number of each mail.

 
Output
For each query, output a sequence of move (E or W) the postman needs to make to deliver the mail. For that E means that the postman should move up the eastern branch and W the western one. If the destination is on the root, just output a blank line would suffice.

Note that for simplicity, we assume the postman always starts from the root regardless of the room he had just visited.

 
Sample Input
2
4
2 1 4 3
3
1 2 3
6
6 5 4 3 2 1
1
1
 
Sample Output
E

WE
EEEEE

模拟了BST的插入。(不加旋转操作方便点)
#include<bits/stdc++.h>
#define F first
#define S second
using namespace std;
const int M = 1e3 + 10 ;
pair<int,int> g[M] ;
int n , x ; void BST (int u) {
if (x < u) {
if (g[u].F == -1) {
g[u].F = x ;
return ;
}
BST (g[u].F) ;
}
else {
if (g[u].S == -1) {
g[u].S = x ;
return ;
}
BST (g[u].S) ;
}
} void dfs (int u) {
if (x == u) {
puts ("") ;
return ;
}
if (x < u) {
printf ("E") ;
dfs (g[u].F) ;
}
else {
printf ("W") ;
dfs (g[u].S) ;
}
} int main () {
int T ;
scanf ("%d" , &T ) ;
while (T --) {
scanf ("%d" , &n) ;
int root ;
scanf ("%d" , &root) ;
for (int i = 1 ; i <= n ; i ++) g[i] = {-1,-1} ;
for (int i = 2 ; i <= n ; i ++) {
scanf ("%d" , &x) ;
BST (root) ;
}
int q ;
scanf ("%d" , &q) ;
while (q --) {
scanf("%d" , &x) ;
dfs (root) ;
}
}
return 0 ;
}

Elven Postman(BST )的更多相关文章

  1. Elven Postman(二叉树)

    Elven Postman Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  2. hdu 5444 Elven Postman(二叉树)——2015 ACM/ICPC Asia Regional Changchun Online

    Problem Description Elves are very peculiar creatures. As we all know, they can live for a very long ...

  3. 【转载】图解:二叉搜索树算法(BST)

    原文:图解:二叉搜索树算法(BST) 摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢!“岁月极美,在于它必然的流逝”“春花 秋月 夏日 冬雪”— ...

  4. C++数据结构之二叉查找树(BST)

    C++数据结构之二叉查找树(BST) 二分查找法在算法家族大类中属于“分治法”,二分查找的过程比较简单,代码见我的另一篇日志,戳这里!因二分查找所涉及的有序表是一个向量,若有插入和删除结点的操作,则维 ...

  5. 【算法】二叉查找树(BST)实现字典API

    参考资料 <算法(java)>                           — — Robert Sedgewick, Kevin Wayne <数据结构>       ...

  6. postman(一)批量执行接口测试用例

    postman(一)批量执行接口测试用例 学习了:https://blog.csdn.net/github_36032947/article/details/78611405 还可以把collecti ...

  7. 数据结构------------------二叉查找树(BST)的java实现

    数据结构------------------二叉查找树(BST)的java实现 二叉查找树(BST)是一种能够将链表插入的灵活性和有序数组查找的高效性相结合的一种数据结构.它的定义如下: 二叉查找树是 ...

  8. 二叉查找树(BST)

    二叉查找树(BST) 二叉查找树(Binary Search Tree)又叫二叉排序树(Binary Sort Tree),它是一种数据结构,支持多种动态集合操作,如 Search.Insert.De ...

  9. 【数据结构】什么是二叉查找树(BST)

    什么是二叉查找树(BST) 1. 什么是BST 对于二叉树中的每个节点X,它的左子树中所有项的值都小于X中的项,它的右子树中所有项的值大于X中的项.这样的二叉树是二叉查找树. 以上是一颗二叉查找树,其 ...

随机推荐

  1. git命令笔记2

    . # 忽略所有后缀为.a的文件 *.a # 不忽略lib.a(相对于上面的) !lib.a # 只忽略根下的TODO,不递归到子目录 /TODO # 忽略所有build/下的文件 build/ # ...

  2. POJ 1365 Prime Land(数论)

    题目链接: 传送门 Prime Land Time Limit: 1000MS     Memory Limit: 10000K Description Everybody in the Prime ...

  3. [Android]Volley源码分析(三)

    上篇看了关于Request的源码,这篇接着来看下RequestQueue的源码. RequestQueue类图:

  4. JS-计算器制作

    不完善,接下来想着把运算符分开成一个一个的按钮... <!DOCTYPE html><html> <head> <meta charset="UTF ...

  5. HTTP常见错误编号

    HTTP 错误 400 400 请求出错 由于语法格式有误,服务器无法理解此请求.不作修改,客户程序就无法重复此请求. HTTP 错误 401 401.1 未授权:登录失败 此错误表明传输给服务器的证 ...

  6. java编程思想-java集合总结-基本概念

    1.java 容器类类库的用途是"保存对象",并将其划分为两个不同的概念: 1)Collection.一个独立元素的序列,这些元素都服从一条或多条规则.List 必须按照插入的顺序 ...

  7. win7怎么显示隐藏文件夹

    1. 点击“组织”,再选择“文件夹和搜索选项”命令. 2. 接下来在打开的“文件夹选项”对话框中,单击“查看”,切换到“查看”选项卡中. 3. 然后在下面的“高级设置”区域,取消“隐藏受保护的操作系统 ...

  8. 三大WEB服务器对比分析(apache ,lighttpd,nginx)

    一.软件介绍(apache  lighttpd  nginx) 1. lighttpd Lighttpd是一个具有非常低的内存开销,cpu占用率低,效能好,以及丰富的模块等特点.lighttpd是众多 ...

  9. linux 查看内存的插槽数

    [root@web ~]# dmidecode|grep -P -A5 "Memory\s+Device"| grep Size | grep -v Range #linux查看内 ...

  10. JAVA第三周课后作业

    JAVA课后作业 一.枚举类型 代码: enum Size{SMALL,MEDIUM,LARGE}; public cl ass EnumTest { public static void main( ...