当前位置:网站首页>4279. 笛卡尔树
4279. 笛卡尔树
2022-07-03 07:00:00 【Ray.C.L】

思路:dfs小根堆建树,记录每一层的节点
代码:
#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;
}
思路: 模拟建树,bfs
代码:
#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;
}
边栏推荐
- Hands on redis master-slave replication, sentinel master-slave switching, cluster sharding
- 【code】偶尔取值、判空、查表、验证等
- Error c2017: illegal escape sequence
- Distributed ID
- 2022 - 06 - 23 vgmp - OSPF - Inter - Domain Security Policy - nat Policy (Update)
- 修改MySQL密码
- crontab定时任务
- Thoughts in Starbucks
- Architecture notes
- In depth analysis of reentrantlock fair lock and unfair lock source code implementation
猜你喜欢

这两种驱蚊成份对宝宝有害,有宝宝的家庭,选购驱蚊产品要注意

Software testing learning - day 3

每日刷題記錄 (十一)

Use the jvisualvm tool ----- tocmat to start JMX monitoring

机器学习 | 简单但是能提升模型效果的特征标准化方法(RobustScaler、MinMaxScaler、StandardScaler 比较和解析)

2022 East China Normal University postgraduate entrance examination machine test questions - detailed solution
![[set theory] partition (partition | partition example | partition and equivalence relationship)](/img/f0/c3c82de52d563f3b81d731ba74e3a2.jpg)
[set theory] partition (partition | partition example | partition and equivalence relationship)

MySQL installation

Golang operation redis: write and read hash type data

POI excel percentage
随机推荐
In depth analysis of reentrantlock fair lock and unfair lock source code implementation
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*
[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones
IC_ EDA_ All virtual machine (rich Edition): questasim, vivado, VCs, Verdi, DC, Pt, spyglass, icc2, synthesize, innovative, ic617, mmsim, process library
PHP install composer
The dynamic analysis and calculation of expressions are really delicious for flee
mongodb
IC_EDA_ALL虚拟机(丰富版):questasim、vivado、vcs、verdi、dc、pt、spyglass、icc2、synplify、INCISIVE、IC617、MMSIM、工艺库
Distributed transactions
保险公司怎么查高血压?
(翻译)异步编程:Async/Await在ASP.NET中的介绍
Jmeter+influxdb+grafana of performance tools to create visual real-time monitoring of pressure measurement -- problem record
Resthighlevelclient gets the mapping of an index
Journal quotidien des questions (11)
dataworks自定义函数开发环境搭建
Software testing learning - the next day
The pressure of large institutions in the bear market has doubled. Will the giant whales such as gray scale, tether and micro strategy become 'giant thunder'?
[day15] introduce the features, advantages and disadvantages of promise, and how to implement it internally. Implement promise by hand
Use the jvisualvm tool ----- tocmat to start JMX monitoring
RestHighLevelClient获取某个索引的mapping