1437D.md (3074B)
1 Source: https://codeforces.com/problemset/problem/1437/D 2 3 D. Minimal Height Tree 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Monocarp had a tree which consisted of $n$ vertices and was rooted at vertex $1$. He decided to study BFS ([Breadth-first search](https://en.wikipedia.org/wiki/Breadth-first_search)), so he ran BFS on his tree, starting from the root. BFS can be described by the following pseudocode: 22 23 ``` 24 a = [] # the order in which vertices were processed 25 q = Queue() 26 q.put(1) # place the root at the end of the queue 27 while not q.empty(): 28 k = q.pop() # retrieve the first vertex from the queue 29 a.append(k) # append k to the end of the sequence in which vertices were visited 30 for y in g[k]: # g[k] is the list of all children of vertex k, sorted in ascending order 31 q.put(y) 32 ``` 33 34 Monocarp was fascinated by BFS so much that, in the end, he lost his tree. Fortunately, he still has a sequence of vertices, in which order vertices were visited by the BFS algorithm (the array a from the pseudocode). Monocarp knows that each vertex was visited exactly once (since they were put and taken from the queue exactly once). Also, he knows that all children of each vertex were viewed in ascending order. 35 36 Monocarp knows that there are many trees (in the general case) with the same visiting order $a$, so he doesn't hope to restore his tree. Monocarp is okay with any tree that has minimum height. 37 38 The height of a tree is the maximum depth of the tree's vertices, and the depth of a vertex is the number of edges in the path from the root to it. For example, the depth of vertex $1$ is $0$, since it's the root, and the depth of all root's children are $1$. 39 40 Help Monocarp to find any tree with given visiting order $a$ and minimum height. 41 42 Input 43 44 The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. 45 46 The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of vertices in the tree. 47 48 The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$; $a_i \neq a_j$; $a_1 = 1$) — the order in which the vertices were visited by the BFS algorithm. 49 50 It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. 51 52 Output 53 54 For each test case print the minimum possible height of a tree with the given visiting order $a$. 55 56 Example 57 58 Input 59 60 ``` 61 3 62 4 63 1 4 3 2 64 2 65 1 2 66 3 67 1 2 3 68 ``` 69 70 Output 71 72 ``` 73 3 74 1 75 1 76 ``` 77 78 Note 79 80 In the first test case, there is only one tree with the given visiting order: 81 82  83 84 In the second test case, there is only one tree with the given visiting order as well: 85 86  87 88 In the third test case, an optimal tree with the given visiting order is shown below: 89 90 