• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
HDU 5444 Elven Postman (二叉树,暴力搜索)

题意:给出一颗二叉树的先序遍历,默认的中序遍历是1.、2、……n。给出q个询问,询问从根节点出发到某个点的路径。

析:本来以为是要建树的,一想,原来不用,其实它给的数是按顺序给的,只要搜结点就行,从根开始搜,如果要到的结点比根结点大,那么一定是向W走,

然后去第一个结点,然后接着判定,一直走,如果找到结束就好。水题。当时想的有点复杂。

代码如下:

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>

using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 2e5 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
int a[maxn];

int main(){
    int T;  cin >> T;
    while(T--){
        scanf("%d", &n);
        for(int i = 0; i < n; ++i){
            scanf("%d", &a[i]);
        }
        scanf("%d", &m);
        while(m--){
            int u;
            scanf("%d", &u);
            int rt = a[0];
            int s = 0;
            while(true){
                if(a[0] == u){    break; }
                if(u > a[s]){
                    printf("W");
                    for(int i = s+1; i < n; ++i){
                        if(a[i] > a[s]){
                            s = i;
                            break;
                        }
                    }
                    if(u == a[s])  break;
                }
                else {
                    printf("E");
                    for(int i = s+1; i < n; ++i){
                        if(a[i] < a[s]){
                            s = i;
                            break;
                        }
                    }
                    if(u == a[s])  break;
                }
            }
            printf("\n");
        }
    }
    return 0;
}

 

posted on 2016-07-30 23:58  dwtfukgv  阅读(332)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3