建立,查询二叉树 hdu 5444
Elven Postman
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2526 Accepted Submission(s): 1493
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.

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.
Note that for simplicity, we assume the postman always starts from the root regardless of the room he had just visited.
4
2 1 4 3
3
1 2 3
6
6 5 4 3 2 1
1
1
WE
EEEEE
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <string>
#include <deque>
#include <vector>
#include <set>
#include <queue>
using namespace std;
#define ll long long
#define P pair<int,int>
const int N =1e3+;
struct Tree{
int w,e,val;
}tree[N];
int t;
int n,q,x,val;
int cnt;
void init(int val){
tree[cnt].w=tree[cnt].e=;
tree[cnt].val=val;
}
void insert(int rt,int val){
if(val>tree[rt].val&&tree[rt].w) insert(tree[rt].w,val);
else if(val<tree[rt].val&&tree[rt].e) insert(tree[rt].e,val);
else{
init(val);
if(val>tree[rt].val) tree[rt].w=cnt;//cnt 不是val ,cnt 是指针
else tree[rt].e=cnt;
cnt++;//加1,每次init 后都要 cnt++
}
}
void query(int rt,int val){
if(tree[rt].val==val) { printf("\n");
return ; }
else if(val>tree[rt].val) {
printf("W");
query(tree[rt].w,val);
}
else{
printf("E");
query(tree[rt].e,val);
}
}
int main()
{
scanf("%d",&t);
while(t--){
scanf("%d",&n);
cnt =;
for(int i=;i<=n;i++){
scanf("%d",&val);
if(i==)
{
init(val);
cnt++; //却忘了加1.
}
else{
insert(,val);
}
}
scanf("%d",&q);
while(q--){
scanf("%d",&x);
query(,x);
}
}
return ;
}
建立,查询二叉树 hdu 5444的更多相关文章
- HDU 5444 Elven Postman 二叉排序树
HDU 5444 题意:给你一棵树的先序遍历,中序遍历默认是1...n,然后q个查询,问根节点到该点的路径(题意挺难懂,还是我太傻逼) 思路:这他妈又是个大水题,可是我还是太傻逼.1000个点的树,居 ...
- LeetCode 606. Construct String from Binary Tree (建立一个二叉树的string)
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- 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 ...
- hdu 5444 Elven Postman 二叉树
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Descrip ...
- HDU 5444 Elven Postman (二叉树,暴力搜索)
题意:给出一颗二叉树的先序遍历,默认的中序遍历是1..2.……n.给出q个询问,询问从根节点出发到某个点的路径. 析:本来以为是要建树的,一想,原来不用,其实它给的数是按顺序给的,只要搜结点就行,从根 ...
- hdu 5444 构建二叉树,搜索二叉树
Elven Postman Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- hdu 5444(构造二叉树然后遍历)
Elven Postman Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- 2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】
Elven Postman Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- hdu 5444 Elven Postman
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Description Elves are very peculia ...
随机推荐
- 漫谈Code Review的错误实践
从刚开始工作时到现在,已经写了7年的代码,大部分代码都被人review过,自己也review了很多人的代码.在上一家公司的时候,我负责的一轮面试是专门进行Code Review的练习和经验谈. 通过在 ...
- Runner启动器
Runner启动器的两种创建方式: 实现ApplicationRunner接口,复写run()方法 实现CommandLineRunner接口,复写run()方法 Runner启动器的执行顺序 如果一 ...
- More than one fragment with the name [spring_web] was found. This is not legal ...
今天在搭建springweb应用环境的时候启动tomcat报错More than one fragment with the name [spring_web] was found. This is ...
- SpringBoot 2.x (13):整合ActiveMQ
ActiveMQ5.x不多做介绍了,主要是SpringBoot的整合 特点: 1)支持来自Java,C,C ++,C#,Ruby,Perl,Python,PHP的各种跨语言客户端和协议 2)支持许多高 ...
- Oracle listener.ora 设置
- 【虚拟机-可用性集】ARM 中可用性集使用的注意事项
Azure 目前有两种部署模型:经典部署模型 (ASM) 和资源管理器 (ARM).如果您之前使用过 ASM 模式下的可用性集,那么很可能在使用 ARM 模式下的可用性集时,会遇到一些问题或者疑惑.这 ...
- 字符串(String)杂谈
作者:臧圩人(zangweiren) 网址:http://zangweiren.javaeye.com >>>转载请注明出处!<<< 上一次我们已经一起回顾了面试题 ...
- Python+selenium 之操作Cookie
在验证浏览器中cookie是否正确时,有时基于真实cookie的测试是无法通过白盒和集成测试进行的.Webdriver提供了操作Cookie的相关方法,可以读取,添加和删除cookie信息. 文本we ...
- HDU 1124 Factorial (阶乘后缀0)
题意: 给一个数n,返回其阶乘结果后缀有几个0. 思路: 首先将n个十进制数进行质因数分解,观察的得到只有2*5才会出现10.那么n!应含有min(2个数,5个数)个后缀0,明显5的个数必定比2少,所 ...
- CodeForces 48C D - The Race (Fraction,数学)
每个加油的站可以确定一个alpha的上下界,比如,第i次加油站a[i],前面加了i次油,a[i]*10≤ alpha*i <(a[i]+1)*10. 取最大的下界,取最小的上界,看看两者之间的满 ...