hdu 5444 Elven Postman

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5444  

Elven Postman

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 \leq 10)$ indicating the number of test cases.

For each test case, there is a number $n\ (n \leq 1000)$ on a line representing the number of rooms in this tree. n integers representing the sequence written at the root follow, respectively $a_1, . . . , a_n$ where $a_1, . . . , a_n ∈ \{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 $x_1, . . . , x_q$ 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

直接用二叉树模拟即可。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<set>
using std::set;
using std::sort;
using std::pair;
using std::swap;
using std::multiset;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 1010;
const int INF = ~0u >> 1;
typedef unsigned long long ull;
struct Node {
	int v;
	Node *ch[2];
	inline void set(int dat, Node *p) {
		v = dat, ch[0] = ch[1] = p;
	}
};
struct BST {
	Node *root, *tail, *null, stack[N];
	inline void init() {
		tail = &stack[0];
		null = tail++;
		null->set(INF, NULL);
		root = null;
	}
	inline Node *newNode(int dat) {
		Node *p = tail++;
		p->set(dat, null);
		return p;
	}
	inline void insert(Node *&x, int dat) {
		if (x == null) { x = newNode(dat); return; }
		insert(x->ch[dat > x->v], dat);
	}
	inline void insert(int dat) {
		insert(root, dat);
	}
	inline void find(int dat) {
		Node *x = root;
		while (x->v != dat) {
			if (dat < x->v) {
				putchar('E');
				x = x->ch[0];
			} else {
				putchar('W');
				x = x->ch[1];
			}
		}
		putchar('\n');
	}
}go;
int main() {
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w+", stdout);
#endif
	int t, n, v, q;
	scanf("%d", &t);
	while (t--) {
		go.init();
		scanf("%d", &n);
		rep(i, n) {
			scanf("%d", &v);
			go.insert(v);
		}
		scanf("%d", &q);
		while (q--) {
			scanf("%d", &v);
			go.find(v);
		}
	}
	return 0;
}

转载于:https://www.cnblogs.com/GadyPu/p/4814534.html

SAP B1中文培訓教材_SDK 公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说 立即下载

相关推荐

200smart 模拟量转换库的建立

模拟量当前值 =(工程值 - 工程量下限)*(模拟量上限 - 模拟量下限)/ (工程量上限 - 工程量下限)+ 模拟量下限2. 建立变量表3. 程序。

mmxxoo_xxmmp的博客 4218

Elven Postman【二叉树还原】

Elven Postman Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 2652    Accepted Submission(s): 1587   Problem Description Elves are very pe...

有关我的科研的足迹、算法竞赛的日子、生活记录。 411

TwinCAT3 实时核异步通信:基于ADS的C++ Server/Client数据交换实战解析

本文深入解析了在TwinCAT3实时核中,如何利用ADS协议实现C++ Server与Client之间的异步数据交换。文章从核心概念入手,详细阐述了实时核、ADS通信及异步机制,并通过实战步骤指导环境搭建、路由配置、Server端服务创建与Client端请求发起,重点剖析了Req/Ind/Res/Con异步回调模型,旨在帮助开发者构建高效、非阻塞的实时工业控制应用。

purple的博客 469

Elven Postman(BST )

Elven Postman Time Limit: 1500/1000 MS (Java/Others)Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 147Accepted Submission(s): 90 Problem Description Elves are very peculi...

weixin_30951231的博客 127

CPRI原理及应用--帧结构

对于CPRI 控制字,协议将1个超帧(256基本帧)的控制数据划分为一个“子通道”,将子通道内超帧内的控制字进行行列划分,分为4列X64行,每64个基本帧组成1列。如下图所示。每个Xs子通道内Ns的定义基本相同,在做控制字设计时,主要已Xs和Ns和X.Y.Z开展控制字映射。

u014035968的博客 2325

汽车TSN时间敏感网络测试技术与探索系列讲座4 -TSN802.1AS同步要求及验证方法

TSN网络各设备类型同步性能要求是什么?各类设备的测试组网及测试内容。也将通过演示来展示相关测试结果,及如何解决上述的测试关注点。

weixin_43903833的博客 761

Elven Postman

题目描述: 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...

博客 313

金仓数据库KCM认证考试介绍【2025年4月更新】

金仓数据库认证资深专家KCM认证考试介绍

qq709869231的博客 3231

Elven Postman(根节点到指定节点的距离)

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 somet...

自渡的博客 531

使用mindie:2.0.RC2-800I-A2-py311-openeuler24.03-lts制作一个通用的模型推理性能测试的镜像

本文详细介绍了在NPU环境下进行模型推理性能测试的完整流程。首先配置了测试所需的环境和设备信息,包括Python 3.10.9和3.11.6版本、测试脚本和镜像文件。然后分步骤说明了镜像制作过程:创建特权容器、安装Python 3.10.9、配置VLLM环境,并详细描述了测试容器的启动方法。最后提供了镜像打包和使用指南,包括如何加载镜像、启动容器、运行测试脚本(以DeepSeek-32B模型为例)以及更换测试模型的步骤。整个过程涉及环境变量配置、权限设置和服务启动等关键操作。

m0_50105717的博客 1015

大学计算 - NumPy库的矩阵运算 实训答案

本文介绍了三个NumPy矩阵操作的实验练习。第一关练习ndarray类型操作,包括矩阵构造、元素平方和计算、矩阵乘积运算和逆矩阵行列式计算;第二关针对matrix类型,涉及绝对值最大值获取、特定矩阵乘积运算和转置矩阵特征值计算;第三关实现Moore-Penrose伪逆矩阵计算,并与NumPy内置pinv函数结果比较误差。实验涵盖了NumPy核心矩阵操作,展示了从基础运算到高级矩阵分解的应用,帮助掌握科学计算中的矩阵处理技巧。

2403_89314916的博客 311

Elven Postman(hdu5444)

题目链接hdu5444 从总根出发,如果比当前根值大往左继续查询且输出W,比当前根值小往右继续查询且输出E,在根就换行并且返回,且每一个快递都是从最顶部出发 #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<string> #include<vector> #include<set> #include<vecto

wxz0v0的博客 164

TIBCO Jaspersoft Studio报表开发 使用教程-进阶版

本文学习内容容器定制二级目录三级目录 容器定制 二级目录 三级目录

吉哲的博客 7521

hdu 5444 Elven Postman(二叉树)

题目链接:hdu 5444 Elven Postman 其实就是一棵二叉搜索树,建完树遍历一下每个节点ans串就出来了。 #include #include #include #include #include #include #include using namespace std; typedef long long ll; const int maxn

不慌不忙、不急不躁 973

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

hdu5444 Elven Postman

hdu5444 Elven Postman

李典金 425

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

HDU 5444 Elven Postman 二叉排序树

HDU 5444   题意:给你一棵树的先序遍历,中序遍历默认是1...n,然后q个查询,问根节点到该点的路径(题意挺难懂,还是我太傻逼) 思路:这他妈又是个大水题,可是我还是太傻逼。1000个点的树,居然用标准二叉树结构来存点,,,卧槽想些什么东西。可以用一维数组,left,right直接指向位置就行了,我这里用的是链表。对于读入的序列,生成一个二叉排序树,同时记录一下路径就可以了,后面居

Macan - 生活点滴 369

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

[HDU 5444]Elven Postman[nlogn建树][BST]

题目链接:[HDU 5444]Elven Postman[nlogn建树] 题意分析: 邮递员要给住在树上的房间送信,房间的安排规律是小的在东,大的在西,查询房间,输出邮递员要走的路径。 解题思路: 根据房间分布规则,在建树的同时把到达这房间的路径记录下来。 这里用了set来建树,初始时放入根结点。根据房间分布规则,如果查询到的房间都不在set中,说明是目前房间都大的,那么把它加入到当前

闲停 614

HDU - 5444 Elven Postman

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 somet...

GJLfly的博客 269

hdu-5444 Elven Postman

题目地址 hdu5444 解题思路 题目就是让你构建一个二叉查找树,然后在寻找路径即可。 AC代码 #include <iostream> using namespace std; struct treeNode { struct treeNode *left, *right; int data; treeNode() { left = NULL; right ...

qq_43227036的博客 185

DFS hdu5444 Elven Postman

传送门:点击打开链接 题意:类似平衡二叉树,告诉你n个节点,以及n个节点的权值,要求右子节点的权值比根节点权值小,左子节点权值比根节点权值大 思路:按照大小建树,顺带保留上一个节点,然后查询的时候只要直接打印路径就行了 #include #include #include #include #include #include #include #include #include #inclu

qwb的博客 896

HDU 5444 Elven Postman

2015长春Online:http://acm.hdu.edu.cn/showproblem.php?pid=5444 建一个二叉树,插入多个点,每次从根节点开始插入,如果当前节点为空则插入当前节点,否则如果小于当前节点的值,则插入右节点,否则插入左节点。 遍历的时候也如此,若找左儿子则输出W,找右儿子输出E,根节点输出空行。 #include struct tree { tree *l

KeyboardPianist的博客 487

hdu5444 Elven Postman(二叉树遍历)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5444 题目大意:给出一颗二叉树的先序遍历,默认的中序遍历是1.、2、……n。给出q个询问,询问从根节点出发到某个点的路径。 思路:根据二叉树的先序和中序遍历可以建立出一颗二叉树,然后遍历二叉树查找即可。 如何遍历?因为中序遍历是从小到大的,所以左子树根据这个遍历即可。 代码: #in

hahahaha 2006

HDU 5444 Elven Postman

Elven Postman Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 3774 Accepted Submission(s): 2261 Problem Description Elves are very peculiar creatures. As we all know, they can live for a very lo

苦鬼·。 208

HDU -5444 Elven Postman

Elven Postman Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Description Elves are very peculiar creatures. As we all know, they can live for a very ...

名字太长容易被发现并打死 431

hdu5444Elven Postman

每插入一个点就从根开始找应该要插到哪里,如果比父亲结点大插到左边,比父亲结点小插到右边,如果他的父亲结点没有左儿子(比父亲大)或者右儿子(比父亲小),就把这个结点作为他的左儿子(比父亲大)或者右儿子(比父亲小)#include <map> #include <set> #include <stack> #include <queue> #include <cmath> #include <ctime

_zidaoziyan的博客 513
上一篇: hdu 5443 The Water Problem
下一篇: hdu 1053 Entropy
dong12276
博客等级 码龄13年 0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值