-
Notifications
You must be signed in to change notification settings - Fork 0
/
D. Minimal Height Tree.cpp
57 lines (53 loc) · 1.25 KB
/
D. Minimal Height Tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <fstream>
#include <string>
#include <iostream>
#include <math.h>
#include <algorithm>
#include<bits/stdc++.h>
#include <vector>
using namespace std;
int t;
int main(){
cin >> t;
for (int z = 0; z < t; z++){
int n;
cin >> n;
int arr[n];
for (int x = 0; x < n; x++){
cin >> arr[x];
}
vector<int> inc;
int c = 0;
inc.push_back(1);
for (int x = 1; x < n; x++){
if (arr[x] > arr[x - 1]){
c++;
}
if (arr[x] < arr[x - 1] || x == n - 1){
inc.push_back(c);
c = 1;
}
if (x == n - 1 && arr[x] < arr[x - 1]){
inc.push_back(1);
}
}
int nc = 0;
int curc = 1;
int answer = 0;
for (int x = 1; x < inc.size(); x++){
curc--;
nc += inc[x];
if (curc == 0){
answer++;
curc = nc;
nc = 0;
}
if (x == inc.size() - 1){
if (nc != 0){
answer++;
}
}
}
cout << answer << "\n";
}
}