CF1437D.Minimal Height Tree

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Monocarp had a tree which consisted of nn vertices and was rooted at vertex 11 . 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:

a = [] # the order in which vertices were processed
q = Queue()
q.put(1) # place the root at the end of the queue
while not q.empty():
    k = q.pop() # retrieve the first vertex from the queue
    a.append(k) # append k to the end of the sequence in which vertices were visited
    for y in g[k]: # g[k] is the list of all children of vertex k, sorted in ascending order
        q.put(y)

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 aa , 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 11 is 00 , since it's the root, and the depth of all root's children are 11 .

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

输入格式

The first line contains a single integer tt ( 1t10001 \le t \le 1000 ) — the number of test cases.

The first line of each test case contains a single integer nn ( 2n21052 \le n \le 2 \cdot 10^5 ) — the number of vertices in the tree.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \dots, a_n ( 1ain1 \le a_i \le n ; aiaja_i \neq a_j ; a1=1a_1 = 1 ) — the order in which the vertices were visited by the BFS algorithm.

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

输出格式

For each test case print the minimum possible height of a tree with the given visiting order aa .

输入输出样例

  • 输入#1

    3
    4
    1 4 3 2
    2
    1 2
    3
    1 2 3

    输出#1

    3
    1
    1

说明/提示

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:

首页