当前位置:网站首页>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;
}
边栏推荐
- Spark practice case (upgraded version)
- Webpack packaging Vue project adds confusion to solve the cache problem
- Introduction to JVM principle
- [Code] sword finger offer 04 search in two-dimensional array
- JS modify the key value of the object array
- centos如何安装mysqldump
- 网工知识角|只需四个步骤,教会你使用SecureCRT连接到eNSP,常用工具操作指南必看
- 使用WebMvcConfigurer进行接口请求拦截进行中增强(附源码)
- The real digital retail should have richer connotation and significance
- Hash (hash)
猜你喜欢

shel自动设置目录权限

JMeter download and installation

Rust:axum learning notes (1) Hello World

Overview of communication protocols

使用kubesphere图形界面dashboard开启devops功能

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

Using webmvcconfigurer to intercept interface requests is being enhanced (with source code)
![[small sample segmentation] msanet: multi similarity and attention guidance for boosting few shot segmentation](/img/b9/270e0f20586a953e83a18f7fac155f.png)
[small sample segmentation] msanet: multi similarity and attention guidance for boosting few shot segmentation

centos如何安装mysqldump

第二轮Okaleido Tiger即将登录Binance NFT,或持续创造销售神绩
随机推荐
Ribbon load balancing principle and some source codes
A. Round Down the Price
Easy to use shell shortcuts
Leetcode:433. minimal genetic change
法解析的外部符号 “public: virtual __cdecl nvinfer1::YoloLayerPlugin::~YoloLayerPlugin(void)“ “public: virtua
利用JSON类型在mysql中实现数组功能
Spark practice case (upgraded version)
微服务的feign调用header头被丢弃两种解决方案(附源码)
一张图看懂KingbaseES V9
Brightcove appoints Dan Freund as chief revenue Officer
What is animation effect? What is the transition effect?
匿名命名管道, 共享内存的进程间通信理解与使用
【无标题】
【软件工程期末复习】知识点+大题详解(E-R图、数据流图、N-S盒图、状态图、活动图、用例图....)
2022-07-26:以下go语言代码输出什么?A:5;B:hello;C:编译错误;D:运行错误。 package main import ( “fmt“ ) type integer in
HEAD detached from origin/...导致push失败
BSN IPFS(星际文件系统)专网简介、功能、架构及特性、接入说明
Ribbon-负载均衡原理及部分源码
Overview of communication protocols
e.target与e.currentTarget的区别