当前位置:网站首页>4279. Cartesian tree
4279. Cartesian tree
2022-07-27 08:38:00 【Pursue people far away】
Cartesian tree It is a binary tree composed of a series of different numbers .
The tree satisfies the nature of heap , Middle order traversal returns the original sequence .
The minimum Cartesian tree represents a Cartesian tree satisfying the property of small root heap .
for example , Given sequence {8,15,3,4,1,5,12,10,18,6}, The generated minimum heap Cartesian tree is shown in the figure .

Now? , Given a length of N The original sequence of , Please generate the minimum heap Cartesian tree , And output its sequence traversal sequence .
Input format
The first line contains integers N.
The second line contains N Two different integers , Represents the original sequence .
Output format
All in one line , Output the sequence traversal sequence of the minimum heap Cartesian tree .
Data range
1≤N≤30,
The value range of elements in the original sequence [−2147483648,2147483647][−2147483648,2147483647].
sample input :
10
8 15 3 4 1 5 12 10 18 6
sample output :
1 3 5 8 4 6 15 10 12 18
Code :
#include <bits/stdc++.h>
using namespace std;
const int N = 40;
int n;
int w[N];
vector<int> level[N];
int getmin(int l, int r)
{
int res = l;
for (int i = l; i <= r; i++)
if (w[res] > w[i])
res = i;
return res;
}
void dfs(int l, int r, int d)
{
if (l > r)
return;
int root = getmin(l, r);
level[d].push_back(w[root]);
dfs(l, root - 1, d + 1);
dfs(root + 1, r, d + 1);
}
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
cin >> w[i];
dfs(0, n - 1, 0);
for (int i = 0; level[i].size(); i++)
for (auto x : level[i])
cout << x << ' ';
return 0;
}
边栏推荐
- Is online account opening safe? Want to know how securities companies get preferential accounts?
- 第2章 前台数据展现
- [netding cup 2020 Qinglong group]areuserialz (buuctf)
- [netding cup 2020 rosefinch group]nmap 1 two solutions
- How to uninstall -- Qianxin secure terminal management system
- Interviewer: what is scaffolding? Why do you need scaffolding? What are the commonly used scaffolds?
- Breadth first search
- Attack and defense world MFW
- 永久设置source的方法
- Element display mode: block level, inline, inline block, nesting specification, display mode conversion
猜你喜欢
随机推荐
MCDF top level verification scheme
regular expression
众昂矿业:新能源行业快速发展,氟化工产品势头强劲
Massive data Xiao Feng: jointly build and govern opengauss root community and share a thriving new ecosystem
P7 Day1 get to know the flask framework
One book 1201 Fibonacci sequence
Implementation of adding function of background user management display
redis的string类型及bitmap
Binglog backup data
[penetration test tool sharing] [dnslog server building guidance]
Flask login implementation
Solution of database migration error
Do a reptile project by yourself
Introduction to depth first search (DFS)
Explain cache consistency and memory barrier
无法获取下列许可SOLIDWORKS Standard,无法找到使用许可文件。(-1,359,2)。
[uni app advanced practice] take you hand-in-hand to learn the development of a purely practical complex project 1/100
3428. 放苹果
第2章 前台数据展现
693. 行程排序







![[NPUCTF2020]ReadlezPHP 1](/img/d9/590446b45f917be3f077a9ea739c20.png)

