当前位置:网站首页>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;
}
边栏推荐
- php安装composer
- Golang operation redis: write and read hash type data
- Sorting out the core ideas of the pyramid principle
- Summary of UI module design and practical application of agent mode
- Troubleshooting of high CPU load but low CPU usage
- golang操作redis:写入、读取hash类型数据
- centos php7.2.24升级到php7.3
- Class and object summary
- mysql误删root账户导致无法登录
- The education of a value investor
猜你喜欢
dataworks自定義函數開發環境搭建
Basic components and intermediate components
Realize PDF to picture conversion with C #
VMware virtual machine C disk expansion
每日刷题记录 (十一)
[set theory] partition (partition | partition example | partition and equivalence relationship)
2022-06-23 vgmp OSPF inter domain security policy NAT policy (under update)
golang操作redis:写入、读取hash类型数据
Software testing learning - the next day
[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones
随机推荐
Search engine Bing Bing advanced search skills
How to plan well?
Setting up the development environment of dataworks custom function
服务器如何设置多界面和装IIS呢?甜甜给你解答!
Unit test notes
10000小时定律不会让你成为编程大师,但至少是个好的起点
萬卷書 - 價值投資者指南 [The Education of a Value Investor]
Laravel frame step pit (I)
10 000 volumes - Guide de l'investisseur en valeur [l'éducation d'un investisseur en valeur]
Sorting out the core ideas of the pyramid principle
Winter vacation work of software engineering practice
Split small interface
New knowledge! The virtual machine network card causes your DNS resolution to slow down
机器学习 | 简单但是能提升模型效果的特征标准化方法(RobustScaler、MinMaxScaler、StandardScaler 比较和解析)
Software testing assignment - day 1
Specified interval inversion in the linked list
[classes and objects] explain classes and objects in simple terms
[Code] if (list! = null & list. Size() > 0) optimization, set empty judgment implementation method
2022 East China Normal University postgraduate entrance examination machine test questions - detailed solution
Unittest attempt