COFO::1437D Minimal Height Tree

Problem

Point

Design

Complexity

Code

#include<bits/stdc++.h>
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for(int i = (a); i <(b); i++)
#define r_rep(i, a, b) for(int i = (a); i >(b); i--)
typedef long long ll;
using namespace std;

void solve(){
	int n; cin >> n;
	vector<int> a(n + 1, 0);
	rep(i, 0, n) cin>> a[i];
	
	int gSz = 1, pos = 1, H = 0;
	while(pos < n){
		int st = pos;
		H++;
		int counted = 0;
		int en = pos;
		rep(i, st, n){
			if (a[i] > a[i+1]) counted++;
			if (counted == gSz) {
				en = i;
				break;
			}
		}
		
		if (counted < gSz) break;
		gSz = en - st +1;
		pos = en + 1;
		
	}
	cout << H << endl;
}
int main(){
    int tc; cin >> tc;
    while(tc--)
        solve();
    return 0;
}