当前位置:网站首页>4279. Cartesian tree
4279. Cartesian tree
2022-07-03 07:03:00 【Ray. C.L】

Ideas :dfs Small root pile tree , Record the nodes of each layer
Code :
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 40;
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()
{
int n;
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;
}
Ideas : Simulation building ,bfs
Code :
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 40;
int w[N];
int q[N];
int L[N], R[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;
}
int dfs(int l, int r){
if(l > r) return -1;
int root = getmin(l,r);
L[root] = dfs(l, root - 1);
R[root] = dfs(root + 1, r);
return root;
}
void bfs(int root){
int hh = 0, tt = 0;
q[0] = root;
while(hh <= tt){
int x = q[hh++];
cout << w[x] << ' ';
if(L[x] != -1) q[++tt] = L[x];
if(R[x] != -1) q[++tt] = R[x];
}
}
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; i++)
cin >> w[i];
int root = dfs(0, n-1);
bfs(root);
return 0;
}
边栏推荐
猜你喜欢

Integration test practice (1) theoretical basis
![[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*](/img/1f/f579110a408c5b5a094733be57ed90.jpg)
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*

golang操作redis:写入、读取hash类型数据

深度学习参数初始化(一)Xavier初始化 含代码

Sorting out the core ideas of the pyramid principle

How to migrate or replicate VMware virtual machine systems
![[classes and objects] explain classes and objects in simple terms](/img/41/250457530880dfe3728432c2ccd50b.png)
[classes and objects] explain classes and objects in simple terms

熊市里的大机构压力倍增,灰度、Tether、微策略等巨鲸会不会成为'巨雷'?

如何迁移或复制VMware虚拟机系统

2022-06-23 VGMP-OSPF-域间安全策略-NAT策略(更新中)
随机推荐
PHP install the spool extension
Software testing learning - the next day
HMS core helps baby bus show high-quality children's digital content to global developers
Search engine Bing Bing advanced search skills
Upgrade CentOS php7.2.24 to php7.3
机械观和系统观的科学思维方式各有什么特点和作用
Abstract learning
Architecture notes
My 2020 summary "don't love the past, indulge in moving forward"
php artisan
Getting started with pytest
Hands on redis master-slave replication, sentinel master-slave switching, cluster sharding
DNS forward query:
instanceof
MySQL syntax (basic)
Inno Setup 制作安装包
10 000 volumes - Guide de l'investisseur en valeur [l'éducation d'un investisseur en valeur]
Basic components and intermediate components
机器学习 | 简单但是能提升模型效果的特征标准化方法(RobustScaler、MinMaxScaler、StandardScaler 比较和解析)
JMeter test result output