当前位置:网站首页>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;
}
边栏推荐
- 众昂矿业:新能源行业快速发展,氟化工产品势头强劲
- CMD command and NPM command
- Day4 --- flask blueprint and rest ful
- Openresty + keepalived to achieve load balancing + IPv6 verification
- MCDF顶层验证方案
- The following license SolidWorks Standard cannot be obtained, and the use license file cannot be found. (-1,359,2)。
- User management - restrictions
- 海关总署:这类产品暂停进口
- Iterators and generators
- Forced login, seven cattle cloud upload pictures
猜你喜欢

如何在qsim查看软件对象的实例?

Forced login, seven cattle cloud upload pictures
![[NPUCTF2020]ReadlezPHP 1](/img/d9/590446b45f917be3f077a9ea739c20.png)
[NPUCTF2020]ReadlezPHP 1

百人参与,openGauss开源社区这群人都在讨论什么?

Day4 --- flask blueprint and rest ful

Realization of background channel group management function

Block, there is a gap between the block elements in the row

Flutter 渲染机制——GPU线程渲染

Massive data Xiao Feng: jointly build and govern opengauss root community and share a thriving new ecosystem

After downloading URL loader and specifying the size of the image with limit, the image will not be displayed
随机推荐
4279. 笛卡尔树
UVM Introduction Experiment 1
pytorch_ demo1
SSTI template injection
[MRCTF2020]Ezpop 1
[geek challenge 2019] finalsql 1
All in one 1319 - queue for water
“寻源到结算“与“采购到付款“两者有什么不同或相似之处?
百人参与,openGauss开源社区这群人都在讨论什么?
Luogu super Mary game
MCDF顶层验证方案
On Valentine's day, I drew an object with characters!
Software test interview questions (key points)
UVM入门实验1
4278. 峰会
while Loop
Pass parameters and returned responses of flask
Use of flask
How to uninstall -- Qianxin secure terminal management system
Explain cache consistency and memory barrier