D. Minimal Height Tree( Educational Codeforces Round 97 (Rated for Div.2))

D. Minimal Height Tree

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

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

The first line of each test case contains a single integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the number of vertices in the tree.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n; ai≠ajai≠aj; a1=1a1=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 2⋅1052⋅105.

Output

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

Example

input

Copy

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

output

Copy

3
1
1

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:

题意:给你n个结点的广度优先搜索的遍历顺序,知道每个结点 的子树都是按照升序排列的,求最少的层数

思路:昨晚做的时候想简单了,觉得这是一道贪心的水题,每次只要看见倒序的就往下加深度,否则返回最开始的一层,就找了最大的降序列的个数输出了,就一直wa。现在才想明白,最开始遇见倒序的一层可能有多个结点,每个结点都可以碰见倒序加深度,昨晚只考虑了最开始遇见倒序层的第一个结点,因为每次遇见降序都要加深度,就容易让人联想到深度优先,但其实还是广度优先,即使加完深度也返回不了第一层,只能返回遇见倒序的第一层,举一个例子

也就是即使遇见了8 9这样的升序也返回不了3那个结点所在的一层,只能返回2那个结点所在的一层

            【 1】

                  【 3】

   【2】          【 8】          【   9】

【6 】          【  5 】           【  4  7 】 


#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
ll a[10001000];
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    ll t;
    cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        for(ll i = 1; i <= n; i ++)  
            cin>>a[i];
        int res = 1, lastcnt = 1, cnt = 1;
        for(int i = 3; i <= n; i ++)
        {
            if(a[i] < a[i - 1])
            {
                if( -- lastcnt == 0)//记录上一层有几个结点,也就是允许后面出现倒序的次数
                {
                    res ++;
                    lastcnt = cnt;
                    cnt = 0;
                }
            }
            cnt ++;
            //printf("a[%d] = %d  cnt = %d  dep = %d\n", i, a[i], cnt, res + 1);
        }
        cout<<res<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值