HDU 5444 Elven Postman 解题报告(水题)

本文介绍了一道关于精灵树上房间编号与遍历的算法题目。通过解析给定的房间序列,找到从树根到达指定房间的路径,涉及树的先序遍历和中序遍历知识。

Elven Postman

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 204    Accepted Submission(s): 114


Problem Description
Elves are very peculiar creatures. As we all know, they can live for a very long time and their magical prowess are not something to be taken lightly. Also, they live on trees. However, there is something about them you may not know. Although delivering stuffs through magical teleportation is extremely convenient (much like emails). They still sometimes prefer other more “traditional” methods.

So, as a elven postman, it is crucial to understand how to deliver the mail to the correct room of the tree. The elven tree always branches into no more than two paths upon intersection, either in the east direction or the west. It coincidentally looks awfully like a binary tree we human computer scientist know. Not only that, when numbering the rooms, they always number the room number from the east-most position to the west. For rooms in the east are usually more preferable and more expensive due to they having the privilege to see the sunrise, which matters a lot in elven culture.

Anyways, the elves usually wrote down all the rooms in a sequence at the root of the tree so that the postman may know how to deliver the mail. The sequence is written as follows, it will go straight to visit the east-most room and write down every room it encountered along the way. After the first room is reached, it will then go to the next unvisited east-most room, writing down every unvisited room on the way as well until all rooms are visited.

Your task is to determine how to reach a certain room given the sequence written on the root.

For instance, the sequence 2, 1, 4, 3 would be written on the root of the following tree.

 

Input
First you are given an integer T(T10) indicating the number of test cases.

For each test case, there is a number n(n1000) on a line representing the number of rooms in this tree. n integers representing the sequence written at the root follow, respectively a1,...,an where a1,...,an{1,...,n} .

On the next line, there is a number q representing the number of mails to be sent. After that, there will be q integers x1,...,xq indicating the destination room number of each mail.
 

Output
For each query, output a sequence of move ( E or W ) the postman needs to make to deliver the mail. For that E means that the postman should move up the eastern branch and W the western one. If the destination is on the root, just output a blank line would suffice.

Note that for simplicity, we assume the postman always starts from the root regardless of the room he had just visited.
 

Sample Input
  
2 4 2 1 4 3 3 1 2 3 6 6 5 4 3 2 1 1 1
 

Sample Output
  
E WE EEEEE
 

Source
 


    解题报告:理解题意后,就会发现其实就是树的先序遍历和中序遍历。而求解答案就更简单了,代码如下:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <functional>
#include <cassert>
#include <bitset>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define ff(i, n) for(int i=0,END=(n);i<END;i++)
#define fff(i, n, m) for(int i=(n),END=(m);i<=END;i++)
#define dff(i, n, m) for(int i=(n),END=(m);i>=END;i--)
#define travel(e, u) for(int e=first[u], v=vv[first[u]]; ~e; e=nxt[e])
#define mid ((l+r)/2)
#define bit(n) (1ll<<(n))
#define clr(a, b) memset(a, b, sizeof(a))
#define debug(x) cout << #x << " = " << x << endl;

#define ls (rt << 1)
#define rs (ls | 1)
#define lson l, m, ls
#define rson m + 1, r, rs

void work();

int main() {
    work();
    return 0;
}

/**************************Beautiful GEGE**********************************/

int a[1111];

void work() {
    int T; scanf("%d", &T);
    fff(cas, 1, T) {
        int n; scanf("%d", &n);
        ff(i, n) scanf("%d", a + i);

        int q; scanf("%d", &q);
        ff(i, q) {
            int f; scanf("%d", &f);
            int mi = 0, ma = n + 1;

            ff(i, n) if (a[i] == f) {
                puts("");
                break;
            } else if (a[i] < f) {
                if (a[i] > mi) {
                    printf("W");
                }
                mi = max(mi, a[i]);
            } else {
                if (a[i] < ma) {
                    printf("E");
                }
                ma = min(ma, a[i]);
            }
        }
    }
}


内容概要 本资源是一套完整可运行的 Qt Widgets 批量图片压缩桌面工具源码,基于 Qt5/C++ 从零开发,专为初学者设计,分步实现图片批量处理全套功能。工具支持多选单张图片、直接读取整个文件夹内所有 JPG/PNG 图像,可自定义输出图片分辨率、调节 JPG0~100 区间压缩质量,自带锁定宽高比防拉伸变形功能;批量处理完成后自动统计每张图片压缩前后文件体积,计算整体压缩缩小比例,直观展示压缩效果。 适用人群 Qt/C++ 零基础初学者,学习 QImage 图像绘图、文件目录遍历、UI 交互开发; 需要本地批量处理图片的办公、设计、自媒体从业者; 想要学习图片缩放、JPG 压缩、本地文件 IO、进度条交互的开发学习者。 使用场景 自媒体批量压缩配图,降低图片体积节省上传流量; 摄影、设计批量统一图片尺寸,批量轻量化相册图片; 程序开发学习:QFileDialog 文件选择、QDir 文件夹遍历、QImage 缩放保存、QSlider 参数联动、批量循环界面防卡顿、文件大小格式化转换全套 Qt 图像开发实战案例。 工具核心功能清单 双模式导入图片:手动多选单张图片 / 一键读取整个文件夹全部图片; 自定义输出宽高分辨率,支持锁定原始宽高比,避免图片拉伸变形; 滑块调节 JPG 压缩质量 0~100,平衡图片清晰度与文件占用大小; 自定义输出保存目录,批量生成压缩后的图片文件; 实时进度条展示处理进度,循环中刷新界面,程序不会假死卡顿; 自动统计每张图片压缩前后体积,换算 KB/MB 直观展示; 批量完成弹窗汇总:图片总数、成功数量、单张大小对比、整体压缩节省空间比例; 完整模块化代码,功能拆分清晰,每段代码附带详细注释,新手可分步拆解学习。 其他说明 开发环境:Qt Creator + Qt5.15 MSVC,Windows 平台可直接编译运行; 源码结构清晰,功能
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值