当前位置:网站首页>PAT 1167 Cartesian Tree(30)
PAT 1167 Cartesian Tree(30)
2022-08-01 12:51:00 【此杭非彼航】
题目


代码
参考代码(有注释)
#include<bits/stdc++.h>
using namespace std;
vector<int> ins;//中序遍历
struct node{
int index, id;
};
vector<node> ans;
bool cmp(node a, node b){
return a.index < b.index;
}
void levelorder(int inl, int inr, int root, int index){
//index层序遍历的下标
if(inl > inr || root < inl || root > inr) return;//超出边界
int i = inl, j = root + 1,minl = ins[inl], minr = ins[root + 1];//左右界限的下标及其对应的值
for(int l = inl; l<root; l++)
if(ins[l] < minl){
//左侧最小值
i = l;
minl = ins[l];
}
for(int p = root + 1; p<= inr; p++){
//右侧最小值
if(ins[p] < minr){
j = p;
minr = ins[p];
}
}
ans.push_back({
index, ins[root]});
levelorder(inl, root - 1, i, 2 * index);//递归左子树
levelorder( root+ 1, inr, j, 2 * index + 1);//递归右子树
}
int main(){
int n, root, minn = INT_MAX, tag;
cin >> n;
ins.resize(n + 1);
for(int i = 1; i <= n; i++){
cin >> ins[i];
if(ins[i] < minn){
//找到根节点
minn = ins[i];
root=i;
}
}
levelorder(1, n, root, 1);
sort(ans.begin(),ans.end(), cmp);
for(int i = 0 ; i < ans.size(); i++)
printf("%d%c", ans[i].id, i < ans.size() - 1? ' ' : '\n');
return 0;
}
复现代码
#include<bits/stdc++.h>
using namespace std;
vector<int> in;
struct node{
int val,index;
};
vector<node> ans;
bool cmp(node x,node y){
return x.index<y.index;
}
void levelorder(int l,int r,int root,int index){
if(l>r||root<l||root>r) return;
int posl=l,posr=r;
for(int i=l+1;i<root;i++){
if(in[i]<in[posl]){
posl=i;
}
}
for(int i=r-1;i>root;i--){
if(in[i]<in[posr]){
posr=i;
}
}
ans.push_back({
in[root],index});
// cout<<in[root]<<endl;
levelorder(l,root-1,posl,index*2);
levelorder(root+1,r,posr,index*2+1);
}
int main()
{
int n,minn=INT_MAX,root=-1;
cin>>n;
in.resize(n);
for(int i=0;i<n;i++){
cin>>in[i];
if(in[i]<minn){
minn=in[i];
root=i;
}
}
// cout<<minn<<" "<<root<<endl;
levelorder(0,n-1,root,1);
sort(ans.begin(),ans.end(),cmp);
for(int i=0;i<ans.size();i++){
cout<<ans[i].val;
if(i!=ans.size()-1) cout<<" ";
}
}
参考文章
边栏推荐
- leetcode:1201. 丑数 III【二分 + 数学 + 容斥原理】
- 【StoneDB Class】Introduction Lesson 2: Analysis of the Overall Architecture of StoneDB
- Beyond Compare 4 试用期到期
- LeetCode_动态规划_中等_377.组合总和 Ⅳ
- 如何设计一个分布式 ID 发号器?
- Based on 10 years of experience in stability assurance, what are the three key questions to be answered in failure recovery?|TakinTalks big coffee sharing
- Fault 007: The dexp derivative is inexplicably interrupted
- SQL function SQUARE
- Meshlab & Open3D SOR filtering
- Batch replace tables in Word with pictures and save
猜你喜欢

搭建LNMT架构

uniapp读取和写入文件

那些利用假期学习的职场人,后来都怎么样了?

bpmn-process-designer基础上进行自定义样式(工具、元素、菜单)

Multi-threaded cases - blocking queue

什么是一致性哈希?可以应用在哪些场景?

CloudCompare&PCL ICP配准(点到面)

Feign 从注册到调用原理分析

Fault 007: The dexp derivative is inexplicably interrupted

shell 中的 分发系统 expect脚本 (传递参数、自动同步文件、指定host和要传输的文件、(构建文件分发系统)(命令批量执行))
随机推荐
VINS-mono 论文解读:IMU预积分+Marg边缘化
NebulaGraph v3.2.0 性能报告
实现集中式身份认证管理的案例
ECCV22|只能11%的参数就能优于Swin,微软提出快速预训练蒸馏方法TinyViT
数据挖掘-03
Dameng replaces the officially authorized dm.key
PIR人体感应AC系列感应器投光灯人体感应开关等应用定制方案
软件设计师考点汇总(室内设计师个人总结)
达梦更换正式授权dm.key
PanGu-Coder:函数级的代码生成模型
消息中间件解析 | 如何正确理解软件应用系统中关于系统通信的那些事?
关于亚马逊测评,你了解多少?
快速幂---学习笔记
故障007:dexp导数莫名中断
求方阵的无穷范数「建议收藏」
硬链接、软连接浅析
浏览器存储
程序员的浪漫七夕
透过开发抽奖小程序,体会创新与迭代
Detailed explanation of table join