当前位置: 首页 > news >正文

CF1490D-Permutation Transformation

CF1490D-Permutation Transformation

题目大意

给你一个长度为 \(n\) 的排列 \(p\) 。最大值为深度为 \(0\) 的点。左边为左子树,右边为右子树。子树中最大值的点则为深度为 \(1\) 的点。以此类推,直到子树为空。

询问是,每一个数字,对应的深度。

题解

直接暴力 \(dfs\) ,每次找到区间内最大值的位置,最大值左边为左子树,右边为右子树,直接模拟即可。

#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define umap unordered_map
#define endl '\n'
using namespace std;
using i128 = __int128;
const int mod =1e9+7;
template <typename T>void read(T&x){x=0;int f = 1;char c=getchar();for(;!isdigit(c);c=getchar())if(c=='-')f=-1;for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);x*=f;
}
template <typename T>void print(T x) {if (x < 0) { putchar('-'); x = -x; }if (x > 9) print(x / 10);putchar(x % 10 + '0');
}
#define int long long
const int N=500005;
const int M=2000005;
int a[105],d[105];
void dfs(int l,int r,int dep)
{if(l>r) return;int maxn=-1,maxpoi;for(int i=l;i<=r;i++){if(a[i]>maxn){maxn=a[i];maxpoi=i;}}d[maxpoi]=dep;dfs(l,maxpoi-1,dep+1);dfs(maxpoi+1,r,dep+1);}
inline void solve()
{int n;cin>>n;for(int i=1;i<=n;i++) cin>>a[i];dfs(1,n,0);for(int i=1;i<=n;i++) cout<<d[i]<<" ";cout<<endl;
}signed main()
{ios;int T=1;cin>>T;for(;T--;) solve();return 0;
}
http://www.jsqmd.com/news/49836/

相关文章:

  • 文本文件与基于二进制文件的存储的学生管理系统
  • Linux 中grep命令在文本中匹配单个的字母
  • Docker Compose 安装问题排查全记录(WSL2 Ubuntu 22.04 环境)
  • 一些 DS
  • newDay22
  • B4324 双向链表
  • 系列最便宜!苹果iPhone 17e要来了:60Hz低刷灵动岛屏幕
  • Codeforces Round 1065 (Div. 3)
  • 代码随想录算法训练营第四天:链表part02
  • CF2027A-Rectangle Arrangement
  • 线段树全家桶
  • 用 Node.js 实现英文数字验证码识别
  • 用 Rust 和 Tesseract OCR 实现英文数字验证码识别
  • 在Java中调用第三方接口并返回第三方页面
  • 251124省运会结束啦
  • 用 C# 和 Tesseract 实现英文数字验证码识别
  • 有了TCP为什么还需要HTTP?再用RPC?这次彻底讲明白了
  • 11.24午夜盘思
  • Java调用第三方接口的方法
  • 2025留学代写危机应对指南:5家靠谱机构助你重返校园
  • 2025美国大学停学应对全攻略:5大靠谱机构助你重返学术轨道
  • 2025美国紧急转学机构推荐深度解析:靠谱机构认准这些核心优势,危机中重启留学之路​
  • 第35天(中等题 数据结构)
  • 2025美国留学求职机构实力解析:你的职场Offer引路人在哪?
  • Universal Fit 3-Button Metal Flip Remote Key (5pcs/lot) – KEYDIY KD NB29-3 for Euro/American Cars
  • 2025美国科研中介TOP5解析:从课题对接至成果落地全程护航
  • 根据缺少的文件查找deb包
  • 第一个Vue2程序
  • 2025美国留学生求职中介TOP5:厚仁教育领衔,精准匹配名企资源
  • CF1097F Alex and a TV Show