Elven Postman---hdu5444(二叉树)
题目链接: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(二叉树)的更多相关文章
- Elven Postman(二叉树)
Elven Postman Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- 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 (二叉树,暴力搜索)
题意:给出一颗二叉树的先序遍历,默认的中序遍历是1..2.……n.给出q个询问,询问从根节点出发到某个点的路径. 析:本来以为是要建树的,一想,原来不用,其实它给的数是按顺序给的,只要搜结点就行,从根 ...
- hdu 5444 Elven Postman 二叉树
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Descrip ...
- (二叉树)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) ...
- hdu 5444 Elven Postman
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Description Elves are very peculia ...
- 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(长春网路赛——平衡二叉树遍历)
题目链接:pid=5444http://">http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Time Limi ...
- Elven Postman(BST )
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= ...
随机推荐
- C语言 · 数对
算法训练 数对 时间限制:1.0s 内存限制:512.0MB 问题描述 编写一个程序,该程序从用户读入一个整数,然后列出所有的数对,每个数对的乘积即为该数. 输入格式:输入只有一行, ...
- 使用info命令查看Redis信息和状态
redis-cli连接服务器后,使用info命令查看Redis信息和状态: ? 1 info 其中memory段显示了redis的内存使用状态. 以下内容复制自:http://redisdoc.com ...
- js入门介绍
为什么起名叫JavaScript?原因是当时Java语言非常红火,所以网景公司希望借Java的名气来推广,但事实上JavaScript除了语法上有点像Java,其他部分基本上没啥关系. 为了让Java ...
- P2P网络穿越 NAT穿越
http://blog.csdn.net/mazidao2008/article/details/4933730 ——————————————————————————————————————————— ...
- 概率dp - UVA 11021 Tribles
Tribles Problem's Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33059 Mean: 有k个细 ...
- 燕十八mysql笔记
mysql复习 一:复习前的准备 1:确认你已安装wamp 2:确认你已安装ecshop,并且ecshop的数据库名为shop 二 基础知识: 1.数据库的连接 mysql -u -p -h -u 用 ...
- MVC 使用IOC实现
实现步骤: 1. 实现IDependencyResolver接口并通过DependencyResolver.SetResolver告知MVC,将部分类型实例解析工作交由IoC容器来处理: using ...
- C++ 类的继承二(赋值兼容性原则)
//赋值兼容性原则 #include<iostream> using namespace std; class PointA{ public: PointA(){ x = ; y = ; ...
- mac环境搭建selenium
前言 搭建python+selenium,mac自带python2.7,需要公司使用的python是3.x,可以自己百度安装python环境. 1. selenium安装 1. selenium的安装 ...
- Spring Cloud对于中小型互联网公司来说是一种福音
Spring Cloud对于中小型互联网公司来说是一种福音,因为这类公司往往没有实力或者没有足够的资金投入去开发自己的分布式系统基础设施,使用Spring Cloud一站式解决方案能在从容应对业务发展 ...