题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5444

 有一个序列,由这个序列可以画出一颗二叉树(每个节点的左边(W)都比它大,右边(E)都比它小),我们每次从树根出发每次向左向右的找到对应的点,求这个过程是怎样的用WE表示;

 

最近刚好看到了数据结构,可以用用指针做

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
#define N 1100
#define INF 0x3f3f3f3f
#define met(a) memset(a, 0, sizeof(a)) struct BST
{
int x;
BST *L, *R;
BST(int m=):x(m)
{
L=NULL;
R=NULL;
};
}a[N], *r; vector<char> path[N];
void Inset(int x)
{
BST *t = r; while(t!=NULL)
{
if(x > t->x)
{
path[x].push_back('W');
if(t->R == NULL)
{
t->R = new BST(x);
break;
}
else
t=t->R;
}
else
{
path[x].push_back('E');
if(t->L == NULL)
{
t->L=new BST(x);
break;
}
else
t=t->L;
}
}
}
int main()
{
int T, n, q, x;
scanf("%d", &T);
while(T--)
{
met(a);
for(int i=; i<N; i++)
path[i].clear();
scanf("%d", &n);
scanf("%d", &x);
r=new BST(x);
for(int i=; i<=n; i++)
{
scanf("%d", &x);
Inset(x);
}
scanf("%d", &q);
while(q--)
{
scanf("%d", &x);
int len=path[x].size();
for(int i=; i<len; i++)
printf("%c", path[x][i]);
printf("\n");
}
}
return ;
}

 后来发现由于大小的关系我们可以直接找;

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <math.h> using namespace std; #define met(a, b) memset(a, b, sizeof(a))
#define N 10003
#define INF 0x3f3f3f3f typedef long long LL; int a[N], n; void solve(int x)
{
char s[N];
met(s, );
int k = , Max = , Min = INF;
for(int i=; i<=n; i++)
{
if(x < a[i] && a[i] < Min)
///当这个点比需要找的点小,并且小于之前遇到的最小的那个说明我们要沿着它的E方向找;
{
s[k++] = 'E';
Min = a[i];
}
else if(x > a[i] && a[i] > Max)
///当这个点比需要找的点大,并且大于之前遇到的最大的那个说明我们要沿着它的W方向找;
{
s[k++] = 'W';
Max = a[i];
}
else if(x == a[i])///当找到时输出并结束;
{
printf("%s\n", s);
return;
}
}
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
met(a, ); scanf("%d", &n);
for(int i=; i<=n; i++)
scanf("%d", &a[i]); int m; scanf("%d", &m);
for(int i=; i<=m; i++)
{
int x;
scanf("%d", &x);
solve(x);
}
}
return ;
}

Elven Postman---hdu5444(二叉树)的更多相关文章

  1. Elven Postman(二叉树)

    Elven Postman Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  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. HDU 5444 Elven Postman (二叉树,暴力搜索)

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

  4. hdu 5444 Elven Postman 二叉树

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

  5. (二叉树)Elven Postman -- HDU -- 54444(2015 ACM/ICPC Asia Regional Changchun Online)

    http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Time Limit: 1500/1000 MS (Java/Others)  ...

  6. hdu 5444 Elven Postman

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

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

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

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

  9. Elven Postman(BST )

    Elven Postman Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  10. Hdu 5444 Elven Postman dfs

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

随机推荐

  1. Python高级编程之生成器(Generator)与coroutine(二):coroutine介绍

    原创作品,转载请注明出处:点我 上一篇文章Python高级编程之生成器(Generator)与coroutine(一):Generator中,我们介绍了什么是Generator,以及写了几个使用Gen ...

  2. codeforces 429 On the Bench dp+排列组合 限制相邻元素,求合法序列数。

    限制相邻元素,求合法序列数. /** 题目:On the Bench 链接:http://codeforces.com/problemset/problem/840/C 题意:求相邻的元素相乘不为平方 ...

  3. Farey Sequence(欧拉函数)

    题意:给出式子F F中分子分母互质,且分子小于分母 例: F2 = {1/2} F3 = {1/3, 1/2, 2/3} F4 = {1/4, 1/3, 1/2, 2/3, 3/4} F5 = {1/ ...

  4. Sys未定义处理方法

    网上很多方法都试过,但是我发现,如果我把iis的端口号换一下,然后重新部署,就正常了,然后你可以再换回原来的端口号,也不会再报错了,应该是微软的一个bug,不知道大伙有什么更好的办法么

  5. 新手入门贴:史上最全Web端即时通讯技术原理详解

    关于IM(InstantMessaging)即时通信类软件(如微信,QQ),大多数都是桌面应用程序或者native应用较为流行,而网上关于原生IM或桌面IM软件类的通信原理介绍也较多,此处不再赘述.而 ...

  6. 后缀数组LCP + 二分 - UVa 11107 Life Forms

    Life Forms Problem's Link Mean: 给你n个串,让你找出出现次数大于n/2的最长公共子串.如果有多个,按字典序排列输出. analyse: 经典题. 直接二分判断答案. 判 ...

  7. 回文树(回文自动机) - URAL 1960 Palindromes and Super Abilities

     Palindromes and Super Abilities Problem's Link: http://acm.timus.ru/problem.aspx?space=1&num=19 ...

  8. mybatis总结(三)之多表查询

    上一节,已经把实体类和配置文件都写过了,这节课直接添加几个方法吧 在DeptMapper.xml文件中添加 <!-- 多表查询(1对多) ,通过部门编号,查询出部门所在的员工姓名,部门名,部门编 ...

  9. Android笔记——Activity中的回传数据案例(装备选择)

    1.创建程序: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  10. JQuery------实现点击左右按钮,切换图片功能

    如图: 代码: html @*商品主图片*@ <div class="product-img" style="display:table-cell;width:40 ...