Records and anti-records
Time limit3sMemory limit1024 MB
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 is a sequence of integers in which every value from to appears exactly once.
A sequence is a subsequence of if it can be obtained by deleting some elements of . That is, and there exist indices with .
- An element is a record if it is strictly greater than all previous elements, that is, for all .
- An element is an anti-record if it is strictly less than all previous elements, that is, for all .
You are given a permutation . Split into two non-empty subsequences and . Every element of must belong to exactly one of them. Maximize the sum of the number of records in and the number of anti-records in .
Input
The first line contains a single integer (), the number of test cases. Each test case consists of two lines.
The first line contains a single integer (), the size of the permutation. The second line contains integers . Each number from to appears exactly once.
The sum of over all test cases does not exceed .
Output
For each test case, print one integer on a separate line: the maximum possible sum of the number of records in and the number of anti-records in over an optimal split.
Hint
In the first test case, one optimal split is shown below. Boxed elements are the records in and the anti-records in .
In the second test case, one optimal split is shown below.