当前位置:网站首页>F - Pre-order and In-order(Atcoder 255)
F - Pre-order and In-order(Atcoder 255)
2022-07-27 04:28:00 【CTGU-Yoghurt】
subject :

Topic link : F - Pre-order and In-order (atcoder.jp)
Answer key :Editorial - Aising Programming Contest 2022(AtCoder Beginner Contest 255)
Ideas :
Because the official explanation has made it very clear
Here I will briefly add some
1. First of all, we need to understand pre-order( Preface ) and in-order( Middle preface ) The concept of
2. Because what we want to build is a binary tree that meets the determination of the preorder and the middle order in the topic
I can build L[10006] and R[10006] Array of
Store separately L[i] and R[i] Left node and right node of
3. When constructing an edge, judge whether it meets the preorder and middle order
It is mainly to judge whether the node is out of bounds
Code details :
#include<iostream>
using namespace std;
const int N = 200006;
int P[N], I[N], Iinv[N];
int L[N], R[N];
bool slove(int s,int e,int S,int E) {//s,e For in p The scope of ,S,E For in i The scope of
int root = P[s], p = Iinv[root];//p Is the position in the sequence
if (p<S || p>E) return 0;// The node is not within the scope of the constructed subtree
if (p - S > 0) {// Able to build left subtree
L[root] = P[s + 1];// With p in s+1 As the root node of the subtree , And recorded in L in
if (!slove(s + 1, e, S, p - 1)) return 0;
// to update [S,P-1] As the new subtree interval range , to update [s+1,e] As
}
if (E - p > 0) {// ditto
R[root] = P[s+p-S+1];//p The first of the right child nodes in
if (!slove(s + p - S + 1, e, p + 1, E)) return 0;
}
return 1;
}
int main(){
int n;
cin >> n;
for (int i = 1; i <=n; i++) cin >> P[i];
for (int i = 1; i <=n; i++) cin >> I[i];
for (int i = 1; i <=n; i++) Iinv[I[i]]=i;
if (P[1] != 1 || !slove(1, n, 1, n)) {//1 Must act as root
cout << -1 << endl;
return 0;
}
for (int i = 1; i <= n; i++) cout << L[i] << " " << R[i]<<endl;
return 0;
}
边栏推荐
- VR panorama gold rush "careful machine" (Part 1)
- [untitled]
- 无有线网络下安装并配置debian
- Network knowledge corner | it only takes four steps to teach you to use SecureCRT to connect to ENSP. You must see the operation guide of common tools
- Hash (hash)
- Redis面试题(2022)
- HEAD detached from origin/...导致push失败
- [machine learning network] BP neural network and deep learning-6 deep neural networks (DNN)
- ROS camera calibration sensor_ Msgs/camerainfo message data type and meaning
- ISG index shows that the it and business service market in the Asia Pacific region fell sharply in the second quarter
猜你喜欢

BSN IPFS(星际文件系统)专网简介、功能、架构及特性、接入说明

无有线网络下安装并配置debian

【C语言】递归详解汉诺塔问题

第二轮Okaleido Tiger即将登录Binance NFT,或持续创造销售神绩

Navicat exports Mysql to table structure and field description

2022-07-26:以下go语言代码输出什么?A:5;B:hello;C:编译错误;D:运行错误。 package main import ( “fmt“ ) type integer in

2022 retraining question bank and answers for main principals of hazardous chemical business units

List Simulation Implementation

项目参数做成可配置项,@ConfigurationProperties注解的使用

法解析的外部符号 “public: virtual __cdecl nvinfer1::YoloLayerPlugin::~YoloLayerPlugin(void)“ “public: virtua
随机推荐
Elastic certification test: 30 day FastPass Study Guide
通信协议综述
The external symbol parsed by the method "public: virtual _ucdecl nvinfer1:: yololayerplugin:: ~yololayerplugin (void)" "public: virtual
xxx is not in the sudoers file. This incident will be reported
What is animation effect? What is the transition effect?
Rust:axum学习笔记(1) hello world
GenericServlet为什么有两个init方法
【比赛参考】PyTorch常用代码段以及操作合集
els 方块显示原理
使用WebMvcConfigurer进行接口请求拦截进行中增强(附源码)
spark练习案例(升级版)
Manually build ABP framework from 0 -abp official complete solution and manually build simplified solution practice
Shel automatically sets directory permissions
Navicat将MySQL导出表结构以及字段说明
Navicat exports Mysql to table structure and field description
数据库泰斗王珊:努力创新,精心打磨优质的数据库产品
P1438 无聊的数列 线段树+差分
JS three methods of traversing arrays: map, foreach, filter
Install and configure Debian on a wired network
452 pages, 130000 words, the overall solution of modern smart Township Xueliang project 2022 Edition