当前位置:网站首页>2022.8.9 Exam arrangement and transformation--1200 questions solution
2022.8.9 Exam arrangement and transformation--1200 questions solution
2022-08-10 03:21:00 【bj_hacker】
题目
3、排列变换–1200
时间限制: | 空间限制:
题目描述:
给出一个大小为 的排列 ,Convert it to a rooted binary tree according to the following rules:
1.The current paragraph(初始时是 中所有数)The number of the largest value in the current subtree(Initially it is the whole tree)the number of the root;
2.All numbers to the left of the maximum position in this segment form the left subtree,Process recursively according to the rules;
3.All numbers to the right of the maximum position in this segment form the right subtree,Process recursively according to the rules.
比如说,排列 The composed binary tree is like this:
root,Its left son is 号,右儿子是 号; No. Left son is 号,右儿子是 号; No. Left son is 号,右
儿子是 号.
Request the depth of each point in the tree(That is, how many edges are there on the simple path from this point to the root,特殊地,根的深度为0).
输入格式:
第一行仅有一个正整数 ( ),表示测试数据的组数.
接下来有 组测试数据:
第一行仅一个正整数 ( ),Indicates the arrangement size;
The second row has a size of 的排列 用空格隔开.
输出格式:
对于每组测试数据,输出一行 个整数用空格隔开,分别表示 号点的深度.
思路
递归
代码实现
#include<bits/stdc++.h>
using namespace std;
const int maxn=100+10;
int t,n;
int a[maxn],h[maxn];
void search(int l,int r){
if(r<l)return;
int temp=0,t=0;
for(int i=l;i<=r;i++){
h[i]++;
if(a[i]>temp){
temp=a[i];
t=i;
}
}
search(l,t-1);
search(t+1,r);
}
int main(){
scanf("%d",&t);
while(t--){
memset(h,-1,sizeof(h));
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
search(1,n);
for(int i=1;i<=n;i++)printf("%d ",h[i]);
printf("\n");
}
return 0;
}
边栏推荐
猜你喜欢
OpenCV图像处理学习一,加载显示修改保存图像相关函数
Deep Learning (5) CNN Convolutional Neural Network
Janus actual production case
【二叉树-中等】1379. 找出克隆二叉树中的相同节点
mysql -sql编程
Process management and task management
OpenCV图像处理学习二,图像掩膜处理
Data Governance (5): Metadata Management
Pagoda server PHP+mysql web page URL jump problem
In automated testing, test data is separated from scripts and parameterized methods
随机推荐
基于C51的中断控制
openpose脚部标注问题梳理
liunx PS1 设置
【论文粗读】(NeurIPS 2020) SwAV:对比聚类结果的无监督视觉特征学习
算法与语音对话方向面试题库
量化交易策略介绍及应用市值中性化选股
网络爬虫错误
MySQL:你做过哪些MySQL的优化?
Open3D 泊松盘网格采样
3dmax如何制作模型走路动画
月薪35K,靠八股文就能做到的事,你居然不知道
OpenCV图像处理学习四,像素的读写操作和图像反差函数操作
【每日一题】1413. 逐步求和得到正数的最小值
OpenCV图像处理学习三,Mat对象构造函数与常用方法
宝塔服务器PHP+mysql网页URL跳转问题
数据库治理利器:动态读写分离
【机器学习】随机森林、AdaBoost、GBDT、XGBoost从零开始理解
桌面云组件介绍与安装
QT中,QTableWidget 使用示例详细说明
【二叉树-中等】687. 最长同值路径