Elven Postman

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

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

 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  5449 5448 5447 5445 5444 

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct BRT{
int date;
BRT *left;
BRT *right;
BRT(){}
BRT(int x):date(x){
left=NULL;
right=NULL;
}
}; void build_tree(BRT *root,int x){
if(x<root->date){
if(root->left==NULL){
root->left=new BRT(x);
return ;
}
build_tree(root->left,x);
}
else{
if(root->right==NULL){
root->right=new BRT(x);
return ;
}
build_tree(root->right,x);
}
} void get_path(BRT *root,int x){
if(root->date==x){
puts("");
return ;
}
else if(x>root->date){
printf("W");
get_path(root->right,x);
}
else{
printf("E");
get_path(root->left,x);
} } int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
int x;
scanf("%d",&x);
BRT *root=new BRT(x);
for(int i=;i<n;i++){
scanf("%d",&x);
build_tree(root,x);
}
int q;
scanf("%d",&q); for(int i=;i<q;i++){
scanf("%d",&x);
get_path(root,x);
}
}
return ;
}

hdu 5444 构建二叉树,搜索二叉树的更多相关文章

  1. HDU 5444 Elven Postman (二叉树,暴力搜索)

    题意:给出一颗二叉树的先序遍历,默认的中序遍历是1..2.……n.给出q个询问,询问从根节点出发到某个点的路径. 析:本来以为是要建树的,一想,原来不用,其实它给的数是按顺序给的,只要搜结点就行,从根 ...

  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. c++ 搜索二叉树 插入,删除,遍历操作

    搜索二叉树是一种具有良好排序和查找性能的二叉树数据结构,包括多种操作,本篇只介绍插入,排序(遍历),和删除操作,重点是删除操作比较复杂,用到的例子也是本人亲自画的 用到的测试图数据例子 第一.构建节点 ...

  4. 【数据结构】——搜索二叉树的插入,查找和删除(递归&非递归)

    一.搜索二叉树的插入,查找,删除 简单说说搜索二叉树概念: 二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值 若它的右 ...

  5. JavaScript树(二) 二叉树搜索

    TypeScript方式实现源码 // 二叉树与二叉树搜索 class Node { key; left; right; constructor(key) { this.key = key; this ...

  6. HDU 5444 Elven Postman 二叉排序树

    HDU 5444 题意:给你一棵树的先序遍历,中序遍历默认是1...n,然后q个查询,问根节点到该点的路径(题意挺难懂,还是我太傻逼) 思路:这他妈又是个大水题,可是我还是太傻逼.1000个点的树,居 ...

  7. Lucene核心--构建Lucene搜索(上篇,理论篇)

    2.1构建Lucene搜索 2.1.1 Lucene内容模型 一个文档(document)就是Lucene建立索引和搜索的原子单元,它由一个或者多个字段(field)组成,字段才是Lucene的真实内 ...

  8. 树&二叉树&&满二叉树&&完全二叉树&&完满二叉树

    目录 树 二叉树 完美二叉树(又名满二叉树)(Perfect Binary Tree) 完全二叉树(Complete Binary Tree) 完满二叉树(Full Binary Tree) 树 名称 ...

  9. Python 和 Elasticsearch 构建简易搜索

    Python 和 Elasticsearch 构建简易搜索 作者:白宁超 2019年5月24日17:22:41 导读:件开发最大的麻烦事之一就是环境配置,操作系统设置,各种库和组件的安装.只有它们都正 ...

随机推荐

  1. 【Troubleshooting Case】Exchange Server 组件状态应用排错?

    在Exchange 2013中,引入了“服务器组件状态”的概念.服务器组件状态从运行环境的角度提供对组成Exchange Server的组件的状态的精细控制. 日常排错时,常常会把Exchange 服 ...

  2. Locust安装教程与使用

    Locust安装教程与使用官网地址:https://github.com/locustio/locust 如果是python3+以上的环境,需要下载locust项目源码进行安装 因Centos7.2环 ...

  3. 【Python图像特征的音乐序列生成】如何生成旋律(大纲),以及整个项目的全部流程

    今天连看三篇论文,不是很细致地看,也没有具体去实现,只是大概明白了一些新思路.这三篇论文,一篇概述了Decoder-Encoder模型,一篇延伸这个模型,首次提出了Attention机制,最后一篇详细 ...

  4. windows7桌面小工具打不开的解决方案

    将任务管理器中的sidebar.exe结束任务: 将C:\Users\用户名\AppData\Local\Microsoft\Windows Sidebar下的settings.ini的文件名修改为任 ...

  5. idea存留

    1.导师匿名评价系统 类似看准网 2.在线降重软件 基于翻译api,或者后期写算法 在线查重导流 公众号: e搜罗

  6. XPath语法规则及实例

    XPath语法规则及实例 XPath语法规则 一.XPath术语: 1.节点:在XPath中,有七种类型的节点:元素.属性.文本.命名空间.处理指令.注释以及文档(根)节点. XML文档是被作为节点树 ...

  7. C++STL概览

    本文转自http://www.cnblogs.com/ggjucheng/archive/2012/01/03/2310884.html 引言 C++ STL可以分为标准容器,算法和函数对象,迭代器和 ...

  8. 【转】Popclip的JSON格式化扩展

    http://liuyunclouder.github.io/2016/09/29/JSONizer:Popclip的JSON格式化扩展 作为一个MAC党,不好好利用MAC的神兵利器,简直就是罪过.A ...

  9. sencha touch 入门学习资料大全

    现在sencha touch已经更新到2.3.1版本了 重新整理一下资料 官方网站:http://www.sencha.com/products/touch/ 在线文档:http://docs.sen ...

  10. fei33423 工作 职场 格言

    对老板: 1.  老板不知道你做的事情(目标设定) 2.  老板要的是规划(对上报告), 自己给自己设定 金字塔四位下的目标,各种维度.如何细化. 2.1 明确老板期望 2.2 与老板达成共识 2.3 ...