A13622 | Unmerge
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Let $a$ and $b$ be two arrays of lengths $n$ and $m$ , respectively, with no elements in common. We can define a new array $\mathrm{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, $\mathrm{merge}(\emptyset,b)=b$ and $\mathrm{merge}(a,\emptyset)=a$ . In particular, $\mathrm{merge}(\emptyset,\emptyset)=\emptyset$ .
- If both arrays are non-empty, and $a_1<b_1$ , then $\mathrm{merge}(a,b)=[a_1]+\mathrm{merge}([a_2,\ldots,a_n],b)$ . That is, we delete the first element $a_1$ of $a$ , merge the remaining arrays, then add $a_1$ to the beginning of the result.
- If both arrays are non-empty, and $a_1>b_1$ , then $\mathrm{merge}(a,b)=[b_1]+\mathrm{merge}(a,[b_2,\ldots,b_m])$ . That is, we delete the first element $b_1$ of $b$ , merge the remaining arrays, then add $b_1$ to the beginning of the result.
This algorithm has the nice property that if $a$ and $b$ are sorted, then $\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]$ and $b=[2,4]$ , then $\mathrm{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=\mathrm{merge}(a,b)$ .
- If one of the arrays is empty, the result is the other array. That is, $\mathrm{merge}(\emptyset,b)=b$ and $\mathrm{merge}(a,\emptyset)=a$ . In particular, $\mathrm{merge}(\emptyset,\emptyset)=\emptyset$ .
- If both arrays are non-empty, and $a_1<b_1$ , then $\mathrm{merge}(a,b)=[a_1]+\mathrm{merge}([a_2,\ldots,a_n],b)$ . That is, we delete the first element $a_1$ of $a$ , merge the remaining arrays, then add $a_1$ to the beginning of the result.
- If both arrays are non-empty, and $a_1>b_1$ , then $\mathrm{merge}(a,b)=[b_1]+\mathrm{merge}(a,[b_2,\ldots,b_m])$ . That is, we delete the first element $b_1$ of $b$ , merge the remaining arrays, then add $b_1$ to the beginning of the result.
This algorithm has the nice property that if $a$ and $b$ are sorted, then $\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]$ and $b=[2,4]$ , then $\mathrm{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=\mathrm{merge}(a,b)$ .
输入格式
The first line contains a single integer $t$ ( $1\le t\le 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\le n\le 2000$ ).
The second line of each test case contains $2n$ integers $p_1,\ldots,p_{2n}$ ( $1\le p_i\le 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$ .
The first line of each test case contains a single integer $n$ ( $1\le n\le 2000$ ).
The second line of each test case contains $2n$ integers $p_1,\ldots,p_{2n}$ ( $1\le p_i\le 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=\mathrm{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]=\mathrm{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]=\mathrm{merge}([3,2,8,4],[6,1,5,7])$ .
In the fourth test case, $[1,2,3,4,5,6]=\mathrm{merge}([1,3,6],[2,4,5])$ , for example.
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]=\mathrm{merge}([3,2,8,4],[6,1,5,7])$ .
In the fourth test case, $[1,2,3,4,5,6]=\mathrm{merge}([1,3,6],[2,4,5])$ , for example.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted