题目传送门

题意:给出线性排列的树,第一个数字是根节点,后面的数如果当前点小或相等往左走,否则往右走,查询一些点走的路径

分析:题意略晦涩,其实就是排序二叉树!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的更多相关文章

  1. hdu 5444 Elven Postman

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Description Elves are very peculia ...

  2. hdu 5444 Elven Postman(长春网路赛——平衡二叉树遍历)

    题目链接:pid=5444http://">http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Time Limi ...

  3. hdu 5444 Elven Postman 二叉树

    Time Limit: 1500/1000 MS (Java/Others)   Memory Limit: 131072/131072 K (Java/Others) Problem Descrip ...

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

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

  6. Hdu 5444 Elven Postman dfs

    Elven Postman Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

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

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

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

  9. HDU 5444 Elven Postman 二叉排序树

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

随机推荐

  1. bash shell和进程

    1 exec builtin 不创建子shell,在原进程的上启动新的脚本,但是它会把老shell的环境清理掉,所以,它从原shell中什么也不继承,在一个干净的环境中执行新的脚本.执行完之后退出当前 ...

  2. 网页Html代码优化及分析

  3. Cooperating sequential processes》,这篇论文提出了大名鼎鼎的概念信号量,Java里面用于线程同步的wait/notify也是信号量的一种实现。

    闲话高并发的那些神话,看京东架构师如何把它拉下神坛 https://mp.weixin.qq.com/s/lAqn8CfSRta9iSvOR1Le6w

  4. mini_magick

    https://github.com/minimagick/minimagick class  https://www.rubydoc.info/github/minimagick/minimagic ...

  5. iOS的基本框架

  6. springboot 多数据源(三种数据库连接池--JDBC,dbcp2,Druid)

    本文使用的是springboot2.0(在配置数据源时和springboot1.X略有区别) 首先:springboot默认支持的连接池有dbcp,dbcp2, tomcat, hikari四种连接池 ...

  7. 关于android的DB操作

    package com.metoo.girls; import android.content.ContentValues; import android.content.Context; impor ...

  8. 【HDU 2089】 不要62

    [题目链接] 点击打开链接 [算法] 数位DP 和上一题 : HDU3555很像 [代码] #include<bits/stdc++.h> using namespace std; #de ...

  9. [laravel]phpunit

    step1.install phpunit composer.json require中增加 "phpunit/phpunit":"4.0.*" 执行 comp ...

  10. [shell test] multiple conditions

      Classic technique (escape metacharacters): if[ \( $g -eq 1-a "$c"="123" \) -o ...