D. Minimal Height Tree

194 篇文章 1 订阅
11 篇文章 0 订阅

https://codeforces.ml/contest/1437/problem/D

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:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e7+10;
const int mod=1e9+7;
ll t,n,l,r,q,p;
ll cnt;
ll a[maxn];
ll dp[maxn];
ll poww(ll a,ll b){
    ll ans=1,base=a;
    while(b!=0){
        if(b&1!=0)
        ans*=base;
        base*=base;
        b>>=1;
    }
    return ans;
}
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
        }
        int ans=1,s=1,k=1;
        for(int i=2;i<n;i++)
        {
            if(a[i]<a[i+1])
            {
                k++;
            }
            else
            {
                s--;
                if(s==0)
                {
                    s=k;
                    k=1;
                    ans++;
                }
                else
                    k++;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值