当前位置:网站首页>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;
}
边栏推荐
- Deep Learning (5) CNN Convolutional Neural Network
- 2022.8.8 Exam Travel Summary
- 小菜鸟河北联通上岗培训随笔
- xss的DOMPurify过滤框架:一个循环问题以及两个循环问题
- Process management and task management
- 2022.8.8考试区域链接(district)题解
- 【机器学习】随机森林、AdaBoost、GBDT、XGBoost从零开始理解
- OpenCV图像处理学习四,像素的读写操作和图像反差函数操作
- Difference Between Data Mining and Data Warehousing
- 在蓝图中给组件动态加子Actor组件
猜你喜欢
随机推荐
宝塔服务器PHP+mysql网页URL跳转问题
2022强网杯 Quals Reverse 部分writeup
Go语言JSON文件的读写操作
数据治理(五):元数据管理
微生物是如何影响身体健康的
SQLserver adds a judgment
gbase 8a数据库如何查看数据或数据文件是否正常?
《GB39707-2020》PDF下载
别再用 offset 和 limit 分页了,性能太差!
【QT】QT项目:自制Wireshark
数据库治理利器:动态读写分离
2022.8.8考试从记忆中写入(memory)题解
51单片机驱动HMI串口屏,串口屏的下载方式
手把手教你搭建ELK-新手必看-第一章:什么是ELK?
将信号与不同开始时间对齐
华为HCIE云计算之FC添加ipsan数据存储
QT中,QTableWidget 使用示例详细说明
OpenCV图像处理学习四,像素的读写操作和图像反差函数操作
C# 正则表达式分组查询
ImportError: Unable to import required dependencies: numpy