CF1381B.Unmerge

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Let aa and bb be two arrays of lengths nn and mm, respectively, with no elements in common. We can define a new array merge(a,b)\mathrm{merge}(a,b) of length n+mn+m recursively as follows:

  • If one of the arrays is empty, the result is the other array. That is, merge(∅,b)=b\mathrm{merge}(\emptyset,b)=b and merge(a,∅)=a\mathrm{merge}(a,\emptyset)=a. In particular, merge(∅,∅)=∅\mathrm{merge}(\emptyset,\emptyset)=\emptyset.
  • If both arrays are non-empty, and a1<b1a_1 \lt b_1, then merge(a,b)=[a1]+merge([a2,…,an],b)\mathrm{merge}(a,b)=[a_1]+\mathrm{merge}([a_2,\ldots,a_n],b). That is, we delete the first element a1a_1 of aa, merge the remaining arrays, then add a1a_1 to the beginning of the result.
  • If both arrays are non-empty, and a1>b1a_1 \gt b_1, then merge(a,b)=[b1]+merge(a,[b2,…,bm])\mathrm{merge}(a,b)=[b_1]+\mathrm{merge}(a,[b_2,\ldots,b_m]). That is, we delete the first element b1b_1 of bb, merge the remaining arrays, then add b1b_1 to the beginning of the result.

This algorithm has the nice property that if aa and bb are sorted, then merge(a,b)\mathrm{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]a=[3,1] and b=[2,4]b=[2,4], then merge(a,b)=[2,3,1,4]\mathrm{merge}(a,b)=[2,3,1,4].

A permutation is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array) and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

There is a permutation pp of length 2n2n. Determine if there exist two arrays aa and bb, each of length nn and with no elements in common, so that p=merge(a,b)p=\mathrm{merge}(a,b).

设 aa 和 bb 分别为长度为 nn 和 mm 的两个数组,且二者没有公共元素。我们可以递归地定义一个长度为 n+mn+m 的新数组 merge(a,b)\mathrm{merge}(a,b) 如下:

  • 若其中一个数组为空,则结果为另一个数组。即,merge(∅,b)=b\mathrm{merge}(\emptyset,b)=b 且 merge(a,∅)=a\mathrm{merge}(a,\emptyset)=a。特别地,merge(∅,∅)=∅\mathrm{merge}(\emptyset,\emptyset)=\emptyset。
  • 若两个数组均非空,且 a1<b1a_1 \lt b_1,则 merge(a,b)=[a1]+merge([a2,…,an],b)\mathrm{merge}(a,b)=[a_1]+\mathrm{merge}([a_2,\ldots,a_n],b)。即:删除 aa 的首元素 a1a_1,对剩余数组进行合并,再将 a1a_1 添加到结果的开头。
  • 若两个数组均非空,且 a1>b1a_1 \gt b_1,则 merge(a,b)=[b1]+merge(a,[b2,…,bm])\mathrm{merge}(a,b)=[b_1]+\mathrm{merge}(a,[b_2,\ldots,b_m])。即:删除 bb 的首元素 b1b_1,对剩余数组进行合并,再将 b1b_1 添加到结果的开头。

该算法具有如下良好性质:若 aa 和 bb 均为升序排列,则 merge(a,b)\mathrm{merge}(a,b) 也必为升序排列。例如,它被用作归并排序(merge-sort)中的子程序。然而,本题中我们将同样考虑该过程作用于非升序排列的数组的情形。例如,若 a=[3,1]a=[3,1] 且 b=[2,4]b=[2,4],则 merge(a,b)=[2,3,1,4]\mathrm{merge}(a,b)=[2,3,1,4]。

一个排列(permutation)是指由 11 到 nn 这 nn 个互不相同的整数以任意顺序组成的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列;但 [1,2,2][1,2,2] 不是排列(数字 22 在数组中出现了两次),[1,3,4][1,3,4] 也不是排列(此时 n=3n=3,但数组中却出现了 44)。

现给定一个长度为 2n2n 的排列 pp。请判断是否存在两个长度均为 nn、且彼此无公共元素的数组 aa 和 bb,使得 p=merge(a,b)p=\mathrm{merge}(a,b)。

输入格式

The first line contains a single integer tt (1≤t≤10001\le t\le 1000) — the number of test cases. Next 2t2t lines contain descriptions of test cases.

The first line of each test case contains a single integer nn (1≤n≤20001\le n\le 2000).

The second line of each test case contains 2n2n integers p1,…,p2np_1,\ldots,p_{2n} (1≤pi≤2n1\le p_i\le 2n). It is guaranteed that pp is a permutation.

It is guaranteed that the sum of nn across all test cases does not exceed 20002000.

第一行包含一个整数 tt(1≤t≤10001\le t\le 1000)—— 表示测试用例的数量。接下来的 2t2t 行包含各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤20001\le n\le 2000)。

每个测试用例的第二行包含 2n2n 个整数 p1,…,p2np_1,\ldots,p_{2n}(1≤pi≤2n1\le p_i\le 2n)。保证 pp 是一个排列。

保证所有测试用例中 nn 的总和不超过 20002000。

输出格式

For each test case, output "YES" if there exist arrays aa, bb, each of length nn and with no common elements, so that p=merge(a,b)p=\mathrm{merge}(a,b). Otherwise, output "NO".

对于每个测试用例,如果存在两个长度均为 nn 且无公共元素的数组 aa 和 bb,使得 p=merge(a,b)p=\mathrm{merge}(a,b),则输出 "YES";否则输出 "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])[2,3,1,4]=\mathrm{merge}([3,1],[2,4]).

In the second test case, we can show that [3,1,2,4][3,1,2,4] is not the merge of two arrays of length 22.

In the third test case, [3,2,6,1,5,7,8,4]=merge([3,2,8,4],[6,1,5,7])[3,2,6,1,5,7,8,4]=\mathrm{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])[1,2,3,4,5,6]=\mathrm{merge}([1,3,6],[2,4,5]), for example.

在第一个测试用例中,[2,3,1,4]=merge([3,1],[2,4])[2,3,1,4]=\mathrm{merge}([3,1],[2,4])。

在第二个测试用例中,我们可以证明 [3,1,2,4][3,1,2,4] 不是两个长度均为 22 的数组的合并结果。

在第三个测试用例中,[3,2,6,1,5,7,8,4]=merge([3,2,8,4],[6,1,5,7])[3,2,6,1,5,7,8,4]=\mathrm{merge}([3,2,8,4],[6,1,5,7])。

在第四个测试用例中,例如 [1,2,3,4,5,6]=merge([1,3,6],[2,4,5])[1,2,3,4,5,6]=\mathrm{merge}([1,3,6],[2,4,5])。

输入解题思路,AI测评打分。不知道怎么写?

首页