当前位置:网站首页>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;
}
边栏推荐
- Principle of bean validation --07
- Is VR panorama just needed now? After reading it, you will understand
- Brightcove任命Dan Freund为首席营收官
- 法解析的外部符号 “public: virtual __cdecl nvinfer1::YoloLayerPlugin::~YoloLayerPlugin(void)“ “public: virtua
- Anonymous named pipes, understanding and use of interprocess communication in shared memory
- 微信input组件添加清除图标,点击清空不生效
- 技术分享 | 需要小心配置的 gtid_mode
- 使用kubesphere图形界面dashboard开启devops功能
- spicy之evt接口定义文件
- JMeter学习笔记004-CSV文件行数控制循环次数
猜你喜欢

微信小程序轮播图

tcp协议知识详解

电商分账系统重要吗,平台应该如何选择分账服务商呢?

Plato Farm全新玩法,套利ePLATO稳获超高收益

【软件工程期末复习】知识点+大题详解(E-R图、数据流图、N-S盒图、状态图、活动图、用例图....)

Delete the whole line of Excel, and delete the pictures together

HEAD detached from origin/...导致push失败

Okaleido tiger will log in to binance NFT in the second round, or continue to create sales achievements

Use the kubesphere graphical interface dashboard to enable the Devops function

使用kubesphere图形界面dashboard开启devops功能
随机推荐
Worship the 321 page PDF of the core technology of Internet entrepreneurship that Alibaba is pushing internally. It's really kneeling
xxx is not in the sudoers file.This incident will be reported.的解决方法
Use the kubesphere graphical interface dashboard to enable the Devops function
Elastic认证考试:30天必过速通学习指南
标准C语言11
452 pages, 130000 words, the overall solution of modern smart Township Xueliang project 2022 Edition
Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns
PX4模块设计之十二:High Resolution Timer设计
What is animation effect? What is the transition effect?
Install and configure Debian on a wired network
Px4 module design 12: high resolution timer design
数据分析师岗位分析
【C语言】递归详解汉诺塔问题
playwright网络爬虫实战案例分享
微服务化解决文库下载业务问题实践
Standard C language 11
VR panorama gold rush "careful machine" (Part 1)
E-commerce system combined with commodity spike activities, VR panorama continues to bring benefits
JMeter learning notes 004-csv file line number control cycle times
ROS camera calibration sensor_ Msgs/camerainfo message data type and meaning