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= ...
随机推荐
- Shell脚本中调用另外一个脚本的方法
(转载): 在Linux平台上开发,经常会在console(控制台)上执行另外一个脚本文件,经常用的方法有:./my.sh 或 source my.sh 或 . my.sh:这三种方法有什么不同呢?我 ...
- cocos2dx遇到的一些坑
针对2.x 1.CCSprite无法直接用文件名更换图片,可以添加如下函数 bool CCSprite::setWithFile(const char *pszFilename) { CCAssert ...
- 连接数据库通过配置文件app.config
ConfigurationManager类 public static class ConfigurationManager 命名空间: System.Configuration 程序集: Syste ...
- Android——配置环境变量
注意:跟java相关的目录不要有中文和空格. 1.打开我的电脑--属性--高级--环境变量 2.新建系统变量JAVA_HOME 和CLASSPATH 变量名:JAVA_HOME 变量值:D:\Java ...
- springmvc+shiro认证框架配置
1,在web.xml中配置fiter,如下所示 <!-- shiro的filter --> <!-- shiro过虑器,DelegatingFilterProxy通过代理模式将spr ...
- JVM中的STW和CMS
Java中Stop-The-World机制简称STW,是在执行垃圾收集算法时,Java应用程序的其他所有线程都被挂起(除了垃圾收集帮助器之外).Java中一种全局暂停现象,全局停顿,所有Java代码停 ...
- xubuntu14.04下编译pjsip及pjsua2 java
Run "./configure" without any options to let the script detect the appropriate settings fo ...
- HeadFirst 设计模式 04 工厂模式
除了 new 操作符之外, 还有更多创造对象的方法. 工厂处理创建对象的细节. 这么做的目的是为了抽象, 例如把创建比萨的代码包装进一个类, 当以后实现改变时, 只需修改这个类即可. 利用静态方法定义 ...
- java 多线程 2 Thread中start()和run()的区别
- hmacSHA1
import javax.crypto.Mac; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; publ ...