当前位置:网站首页>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;
}
边栏推荐
- How does the insurance company check hypertension?
- In depth analysis of reentrantlock fair lock and unfair lock source code implementation
- Software testing assignment - day 1
- UTC time, GMT time, CST time
- mysql误删root账户导致无法登录
- Pytest -- write and manage test cases
- How can I split a string at the first occurrence of “-” (minus sign) into two $vars with PHP?
- Application scenarios of Catalan number
- Personally design a highly concurrent seckill system
- dataworks自定義函數開發環境搭建
猜你喜欢

Distributed transactions

MySQL installation

Interfaces and related concepts

Summary of the design and implementation of the weapon system similar to the paladin of vitality
![[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones](/img/d0/850e095a43610366d6144b2471f3f7.jpg)
[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones

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'?

New knowledge! The virtual machine network card causes your DNS resolution to slow down

Arctic code vault contributor

每日刷题记录 (十一)

Summary of remote connection of MySQL
随机推荐
服务器如何设置多界面和装IIS呢?甜甜给你解答!
Resttemplate configuration use
Software testing assignment - day 3
Use the jvisualvm tool ----- tocmat to start JMX monitoring
多个全局异常处理类,怎么规定执行顺序
10 000 volumes - Guide de l'investisseur en valeur [l'éducation d'un investisseur en valeur]
Golang operation redis: write and read kV data
Stream stream
[day15] introduce the features, advantages and disadvantages of promise, and how to implement it internally. Implement promise by hand
2022-06-23 vgmp OSPF inter domain security policy NAT policy (under update)
How does the insurance company check hypertension?
这两种驱蚊成份对宝宝有害,有宝宝的家庭,选购驱蚊产品要注意
Laravel frame step pit (I)
万卷书 - 价值投资者指南 [The Education of a Value Investor]
Jmeter+influxdb+grafana of performance tools to create visual real-time monitoring of pressure measurement -- problem record
保险公司怎么查高血压?
UTC time, GMT time, CST time
[set theory] partition (partition | partition example | partition and equivalence relationship)
Book recommendation~
Software testing learning - day 3