当前位置:网站首页>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<<" ";
}
}
参考文章
边栏推荐
- What is consistent hashing?In what scenarios can it be applied?
- CloudCompare&PCL ICP配准(点到面)
- 如何使用OpenCV测量图像中物体之间的距离
- Do wildcard SSL certificates not support multiple domains?
- 芝加哥丰田技术学院 | Leveraging Natural Supervision for Language Representation Learning and Generation(利用自然监督进行语言表示学习和生成)
- Dapr 与 NestJs ,实战编写一个 Pub & Sub 装饰器
- How do we do full-link grayscale on the database?
- JMP Pro 16.0软件安装包下载及安装教程
- postgresql之page分配管理(一)
- 那些利用假期学习的职场人,后来都怎么样了?
猜你喜欢
那些利用假期学习的职场人,后来都怎么样了?
库函数的模拟实现(strlen)(strcpy)(strcat)(strcmp)(strstr)(memcpy)(memmove)(C语言)(VS)
NebulaGraph v3.2.0 性能报告
HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
并发编程10大坑,你踩过几个?
PyTorch 进阶之路:在 GPU 上训练深度神经网络
人像分割技术解析与应用
Multi-threaded cases - blocking queue
什么是一致性哈希?可以应用在哪些场景?
Fault 007: The dexp derivative is inexplicably interrupted
随机推荐
MMF的初步介绍:一个规范化的视觉-语言多模态任务框架
故障007:dexp导数莫名中断
formatdatetime函数 mysql(date sub函数)
[Unity3D Plugin] AVPro Video Plugin Share "Video Player Plugin"
动态库、静态库浅析
Istio投入生产的障碍以及如何解决这些问题
Meshlab&Open3D SOR滤波
脚本语言Lua的基础知识总结
如何使用 Authing 单点登录,集成 Discourse 论坛?
DDL和DML的含义与区别「建议收藏」
论文笔记All about Eve: Execute-Verify Replication for Multi-Core Servers
8. How does the SAP ABAP OData service support the Create operation
易周金融分析 | 银行ATM机智能化改造提速;互联网贷款新规带来挑战
This article will take you to thoroughly clarify the working mechanism of certificates in Isito
postgresql之page分配管理(一)
Batch replace tables in Word with pictures and save
The basic knowledge of scripting language Lua summary
芝加哥丰田技术学院 | Leveraging Natural Supervision for Language Representation Learning and Generation(利用自然监督进行语言表示学习和生成)
计算器:中缀表达式转后缀表达式
浏览器存储