当前位置:网站首页>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;
}
边栏推荐
- Dbnet: real time scene text detection with differentiable binarization
- In depth analysis of reentrantlock fair lock and unfair lock source code implementation
- 机器学习 | 简单但是能提升模型效果的特征标准化方法(RobustScaler、MinMaxScaler、StandardScaler 比较和解析)
- Golang operation redis: write and read hash type data
- dataworks自定义函数开发环境搭建
- PHP install composer
- Sorting out the core ideas of the pyramid principle
- Advanced API (local simulation download file)
- How does the insurance company check hypertension?
- Advanced API (serialization & deserialization)
猜你喜欢

JUC forkjoinpool branch merge framework - work theft

Distributed transactions

2022-06-23 VGMP-OSPF-域间安全策略-NAT策略(更新中)
![[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)*

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

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

Dbnet: real time scene text detection with differentiable binarization

Jmeter+influxdb+grafana of performance tools to create visual real-time monitoring of pressure measurement -- problem record

10000小時定律不會讓你成為編程大師,但至少是個好的起點

2022-06-23 VGMP-OSPF-域間安全策略-NAT策略(更新中)
随机推荐
Distributed transactions
Pits encountered in the use of El checkbox group
Interface learning
Advanced API (serialization & deserialization)
Golang operation redis: write and read kV data
[Fiddler problem] solve the problem about Fiddler's packet capturing. After the mobile network is configured with an agent, it cannot access the Internet
Shim and Polyfill in [concept collection]
Realize PDF to picture conversion with C #
Specified interval inversion in the linked list
10000小時定律不會讓你成為編程大師,但至少是個好的起點
How can I split a string at the first occurrence of “-” (minus sign) into two $vars with PHP?
php安装composer
MySQL syntax (basic)
php安装swoole扩展
2021 year end summary
[day15] introduce the features, advantages and disadvantages of promise, and how to implement it internally. Implement promise by hand
The 10000 hour rule won't make you a master programmer, but at least it's a good starting point
What are the characteristics and functions of the scientific thinking mode of mechanical view and system view
Pytest attempts to execute the test case without skipping, but the case shows that it is all skipped
Asynchronous programming: async/await in asp Net