排序二叉树 HDOJ 5444 Elven Postman
题意:给出线性排列的树,第一个数字是根节点,后面的数如果当前点小或相等往左走,否则往右走,查询一些点走的路径
分析:题意略晦涩,其实就是排序二叉树!1<<1000 普通数组开不下,用指针省内存。将树倒过来好理解些
E---------------------------------------W
2
/ \
1 4
/
3 6
/
5
/
4
/
3
/
2
/
1
E---------------------------------------W
收获:排序二叉树插入和查询
代码:
/************************************************
* Author :Running_Time
* Created Time :2015/9/14 星期一 15:34:35
* File Name :H.cpp
************************************************/
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e3 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int a[N], q[N];
struct BST {
int val;
BST *left, *right;
BST *insert(BST *p, int x) {
if (p == NULL) {
BST *t = new BST;
t->val = x;
t->left = t->right = NULL;
return t;
}
if (x <= p->val) p->left = insert (p->left, x);
else p->right = insert (p->right, x);
return p;
}
bool find(BST *p, int x) {
if (x == p->val) return true;
else if (p == NULL) return false;
else if (x <= p->val) {
cout << "E";
return find (p->left, x);
}
else {
cout << "W";
return find (p->right, x);
}
}
}bst; int main(void) {
ios::sync_with_stdio (false);
int T; cin >> T;
while (T--) {
int n; cin >> n;
for (int i=1; i<=n; ++i) {
cin >> a[i];
}
int m; cin >> m;
for (int i=1; i<=m; ++i) {
cin >> q[i];
}
BST *root = NULL;
for (int i=1; i<=n; ++i) {
root = bst.insert (root, a[i]);
}
for (int i=1; i<=m; ++i) {
bst.find (root, q[i]);
cout << endl;
}
} return 0;
}
排序二叉树 HDOJ 5444 Elven Postman的更多相关文章
- hdu 5444 Elven Postman
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Description Elves are very peculia ...
- hdu 5444 Elven Postman(长春网路赛——平衡二叉树遍历)
题目链接:pid=5444http://">http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Time Limi ...
- hdu 5444 Elven Postman 二叉树
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Descrip ...
- 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 ...
- 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 dfs
Elven Postman Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- HDU 5444 Elven Postman (2015 ACM/ICPC Asia Regional Changchun Online)
Elven Postman Elves are very peculiar creatures. As we all know, they can live for a very long time ...
- HDU 5444 Elven Postman (二叉树,暴力搜索)
题意:给出一颗二叉树的先序遍历,默认的中序遍历是1..2.……n.给出q个询问,询问从根节点出发到某个点的路径. 析:本来以为是要建树的,一想,原来不用,其实它给的数是按顺序给的,只要搜结点就行,从根 ...
- HDU 5444 Elven Postman 二叉排序树
HDU 5444 题意:给你一棵树的先序遍历,中序遍历默认是1...n,然后q个查询,问根节点到该点的路径(题意挺难懂,还是我太傻逼) 思路:这他妈又是个大水题,可是我还是太傻逼.1000个点的树,居 ...
随机推荐
- 服务器返回JSON,IE出现下载问题
我向来的观点,IE就是个奇葩. 服务器返回json,chrome处理得好地地,但IE却奇葩地向你请求是否要保存这个JSON文件? 之所以出现这种弱智现象,是因为IE无法识别一个所谓的响应头部:appl ...
- PrintWrite
向文本输出流打印对象的格式化表示形式.此类实现在 PrintStream 中的所有 print 方法.它不包含用于写入原始字节的方法,对于这些字节,程序应该使用未编码的字节流进行写入. 与 Print ...
- Codeforces Beta Round #25 (Div. 2 Only)D. Roads not only in Berland
D. Roads not only in Berland time limit per test 2 seconds memory limit per test 256 megabytes input ...
- django错误 - Reason given for failure: CSRF cookie not set.
练习Django表单提交时遇到如下问题: 在网上各种查找,终于找到了解决方法. 1.在from 表单中添加 {% csrf_token %} 2.在视图中添加 from django.template ...
- codeforces 669C C. Little Artem and Matrix(水题)
题目链接: C. Little Artem and Matrix time limit per test 2 seconds memory limit per test 256 megabytes i ...
- Struts2的各种标签库
1 在JSP中使用taglib编译指令导入标签库 <%@ taglib prefix="s" uri="/struts-tags" %> ----- ...
- python(二):使用multiprocessing中的常见问题
简介在python的解释器中,CPython是应用范围最广的一种,其具有丰富的扩展包,方便了开发者的使用.当然CPython也不是完美的,由于全局解释锁(GIL)的存在,python的多线程可以近似看 ...
- noip2010引水入城
https://www.zybuluo.com/ysner/note/1334997 这道题fst了 题面 戳我 解析 我一开始的想法是,按照高度给第一行排序,然后贪心地选取目前到不了的,高度最高的第 ...
- 廖雪峰python3练习题一
数据类型和变量 题目: 答案: print(123) print(456.789) print('\'Hello,world\'') print('\'Hello,\\\'Adam\\\'\'') p ...
- docker容器基础
一.docker容器基础6种名称空间:UTS.MOunt.IPC.PID.User.Net (1) Linux Namespaces:namespace 系统调用参数 隔离内容 内核版本 UTS ...