Minimal Height Tree
Contest: Educational Codeforces Round 97 (Rated for Div. 2)
Difficulty rating: 1600
Tags: graphs, greedy, shortest paths, trees

Monocarp had a tree which consisted of $$$n$$$ vertices and was rooted at vertex $$$1$$$. He decided to study BFS (Breadth-first search), so he ran BFS on his tree, starting from the root. BFS can be described by the following pseudocode:

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.

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.

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$$$.

Help Monocarp to find any tree with given visiting order $$$a$$$ and minimum height.

Input format:
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases.

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.

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.

It's guaranteed that the total sum of $$$n$$$ over test cases doesn't exceed $$$2 \cdot 10^5$$$.

Output format:
For each test case print the minimum possible height of a tree with the given visiting order $$$a$$$.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first test case, there is only one tree with the given visiting order:

In the second test case, there is only one tree with the given visiting order as well:

In the third test case, an optimal tree with the given visiting order is shown below:

Example input:
3
4
1 4 3 2
2
1 2
3
1 2 3

Example output:
3
1
1

Editorial:
Due to the nature of BFS, the visiting order consists of several segments: first goes root (has depth $$$0$$$), then all vertices with depth $$$1$$$, then all vertices with depth $$$2$$$ and so on. Since any vertex of depth $$$d$$$ is a child of vertex of depth $$$d - 1$$$, then it's optimal to make the number of vertices with depth $$$1$$$ as many as possible, then make the number of vertices with depth $$$2$$$ as many as possible and so on.
Since children of a vertex are viewed in ascending order and form a segment in visiting order then an arbitrary segment of visiting order can be children of the same vertex iff elements in the segments are in ascending order.
These two observations lead us to a greedy strategy: $$$a_1 = 1$$$, then let's find the maximum $$$r_1$$$ that segment $$$a_2, \dots, a_{r_1}$$$ is in ascending order — they will be the children of $$$a_1$$$ and the only vertices of depth $$$1$$$. Next search the maximum $$$r_2$$$ such that segment $$$a_{r_1 + 1}, \dots, a_{r_2}$$$ is in ascending order — they will be the children of $$$a_2$$$, and so on. It's easy to see that this strategy maximizes the number of vertices of each depth level, so minimize the height of the tree.