题目中说每个点的子结点都是升序排列的,并且1结点是父结点,所以我们可以从第二层开始,统计第二层呈升序排列的点的个数,该个数即为第二层的结点数。然后以第二层为基础依次统计每层升序排列的点的个数,当第i层结点不为升序排列的点的个数超过第i-1层结点的个数时,则意味着该层结点已满,深度需要加1。
#include<bits/stdc++.h> #define x first #define y second #define PB push_back #define mst(x,a) memset(x,a,sizeof(x)) #define all(a) begin(a),end(a) #define rep(x,l,u) for(ll x=l;x<u;x++) #define rrep(x,l,u) for(ll x=l;x>=u;x--) #define sz(x) x.size() #define IOS ios::sync_with_stdio(false);cin.tie(0); using namespace std; typedef unsigned long long ull; typedef pair<int,int> PII; typedef pair<char,char> PCC; typedef long long ll; typedef pair<ll,ll> PLL; const int N=2*1e5+10; const int M=1e5+10; const int INF=0x3f3f3f3f; const int mod=1e9+7; int a[N]; void solve(){ int n;cin>>n; rep(i,1,n+1) cin>>a[i]; if(is_sorted(a+1,a+n+1)) cout<<1<<endl; else{ int ans=0,cnt=0,cur=1,last=1; bool flag=true; rep(i,3,n+1){ if(a[i]>a[i-1]){ cur++; continue; } cnt++; if(cnt==last){ ans++; cnt=0; last=cur; cur=1; }else cur++; } cout<<ans+1<<endl; } } int main(){ IOS; //freopen("test.txt", "r", stdin); //freopen("test.txt", "w", stdout); int t;cin>>t; while(t--) solve(); return 0; }
原创声明,本文系作者授权云+社区发表,未经许可,不得转载。
如有侵权,请联系 yunjia_community@tencent.com 删除。
我来说两句