CF1381B.Unmerge
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Let a and b be two arrays of lengths n and m , respectively, with no elements in common. We can define a new array merge(a,b) of length n+m recursively as follows:
- If one of the arrays is empty, the result is the other array. That is, merge(∅,b)=b and merge(a,∅)=a . In particular, merge(∅,∅)=∅ .
- If both arrays are non-empty, and a1<b1 , then merge(a,b)=[a1]+merge([a2,…,an],b) . That is, we delete the first element a1 of a , merge the remaining arrays, then add a1 to the beginning of the result.
- If both arrays are non-empty, and a1>b1 , then merge(a,b)=[b1]+merge(a,[b2,…,bm]) . That is, we delete the first element b1 of b , merge the remaining arrays, then add b1 to the beginning of the result.
This algorithm has the nice property that if a and b are sorted, then merge(a,b) will also be sorted. For example, it is used as a subroutine in merge-sort. For this problem, however, we will consider the same procedure acting on non-sorted arrays as well. For example, if a=[3,1] and b=[2,4] , then merge(a,b)=[2,3,1,4] .
A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation ( 2 appears twice in the array) and [1,3,4] is also not a permutation ( n=3 but there is 4 in the array).
There is a permutation p of length 2n . Determine if there exist two arrays a and b , each of length n and with no elements in common, so that p=merge(a,b) .
输入格式
The first line contains a single integer t ( 1≤t≤1000 ) — the number of test cases. Next 2t lines contain descriptions of test cases.
The first line of each test case contains a single integer n ( 1≤n≤2000 ).
The second line of each test case contains 2n integers p1,…,p2n ( 1≤pi≤2n ). It is guaranteed that p is a permutation.
It is guaranteed that the sum of n across all test cases does not exceed 2000 .
输出格式
For each test case, output "YES" if there exist arrays a , b , each of length n and with no common elements, so that p=merge(a,b) . Otherwise, output "NO".
输入输出样例
输入#1
6 2 2 3 1 4 2 3 1 2 4 4 3 2 6 1 5 7 8 4 3 1 2 3 4 5 6 4 6 1 3 7 4 5 8 2 6 4 3 2 5 1 11 9 12 8 6 10 7
输出#1
YES NO YES YES NO NO
说明/提示
In the first test case, [2,3,1,4]=merge([3,1],[2,4]) .
In the second test case, we can show that [3,1,2,4] is not the merge of two arrays of length 2 .
In the third test case, [3,2,6,1,5,7,8,4]=merge([3,2,8,4],[6,1,5,7]) .
In the fourth test case, [1,2,3,4,5,6]=merge([1,3,6],[2,4,5]) , for example.