Under construction

Still being built. Things may change

Records and anti-records

Time limit3sMemory limit1024 MB

Summary
Split a permutation into two non-empty subsequences to maximize the records in the first plus the anti-records in the second.
Level

Hard8 of 10

Topics
Dynamic programming, Segment tree, Greedy
Solved
No attempts yet

Problem

A permutation of size nn is a sequence of integers a1,a2,…,ana_1, a_2, \ldots, a_n in which every value from 11 to nn appears exactly once.

A sequence b1,b2,…,blb_1, b_2, \ldots, b_l is a subsequence of a1,a2,…,ana_1, a_2, \ldots, a_n if it can be obtained by deleting some elements of aa. That is, l≤nl \le n and there exist indices i1<i2<…<ili_1 < i_2 < \ldots < i_l with ait=bta_{i_t} = b_t.

  • An element aia_i is a record if it is strictly greater than all previous elements, that is, aj<aia_j < a_i for all 1≤j<i1 \le j < i.
  • An element aia_i is an anti-record if it is strictly less than all previous elements, that is, aj>aia_j > a_i for all 1≤j<i1 \le j < i.

You are given a permutation p1,p2,…,pnp_1, p_2, \ldots, p_n. Split pp into two non-empty subsequences qq and rr. Every element of pp must belong to exactly one of them. Maximize the sum of the number of records in qq and the number of anti-records in rr.

Input

The first line contains a single integer tt (1≤t≤100 0001 \le t \le 100\,000), the number of test cases. Each test case consists of two lines.

The first line contains a single integer nn (2≤n≤200 0002 \le n \le 200\,000), the size of the permutation. The second line contains nn integers p1,p2,…,pnp_1, p_2, \ldots, p_n. Each number from 11 to nn appears exactly once.

The sum of nn over all test cases does not exceed 200 000200\,000.

Output

For each test case, print one integer on a separate line: the maximum possible sum of the number of records in qq and the number of anti-records in rr over an optimal split.

Hint

In the first test case, one optimal split is shown below. Boxed elements are the records in qq and the anti-records in rr.

  • q=1 2 3 5q = \boxed{1}\ \boxed{2}\ \boxed{3}\ \boxed{5}
  • r=4r = \boxed{4}

In the second test case, one optimal split is shown below.

  • q=3 8 4 1 2 9q = \boxed{3}\ \boxed{8}\ 4\ 1\ 2\ \boxed{9}
  • r=10 7 5 6r = \boxed{10}\ \boxed{7}\ \boxed{5}\ 6

Examples1

  1. Example 1

    Input
    4
    5
    4 1 2 3 5
    10
    3 8 10 4 1 2 7 9 5 6
    3
    1 2 3
    6
    4 2 5 1 6 3
    
    Expected output
    5
    6
    3
    5