A15802 | Subsequence Addition (Hard Version)
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
The only difference between the two versions is that in this version, the constraints are higher.
Initially, array $a$ contains just the number $1$ . You can perform several operations in order to change the array. In an operation, you can select some subsequence $^{\dagger}$ of $a$ and add into $a$ an element equal to the sum of all elements of the subsequence.
You are given a final array $c$ . Check if $c$ can be obtained from the initial array $a$ by performing some number (possibly 0) of operations on the initial array.
$^{\dagger}$ A sequence $b$ is a subsequence of a sequence $a$ if $b$ can be obtained from $a$ by the deletion of several (possibly zero, but not all) elements. In other words, select $k$ ( $1 \leq k \leq |a|$ ) distinct indices $i_1, i_2, \dots, i_k$ and insert anywhere into $a$ a new element with the value equal to $a_{i_1} + a_{i_2} + \dots + a_{i_k}$ .
Initially, array $a$ contains just the number $1$ . You can perform several operations in order to change the array. In an operation, you can select some subsequence $^{\dagger}$ of $a$ and add into $a$ an element equal to the sum of all elements of the subsequence.
You are given a final array $c$ . Check if $c$ can be obtained from the initial array $a$ by performing some number (possibly 0) of operations on the initial array.
$^{\dagger}$ A sequence $b$ is a subsequence of a sequence $a$ if $b$ can be obtained from $a$ by the deletion of several (possibly zero, but not all) elements. In other words, select $k$ ( $1 \leq k \leq |a|$ ) distinct indices $i_1, i_2, \dots, i_k$ and insert anywhere into $a$ a new element with the value equal to $a_{i_1} + a_{i_2} + \dots + a_{i_k}$ .
输入格式
The first line of the input contains an integer $t$ ( $1 \leq t \leq 1000$ ) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ( $1 \leq n \leq 2 \cdot 10^5$ ) — the number of elements the final array $c$ should have.
The second line of each test case contains $n$ space-separated integers $c_i$ ( $1 \leq c_i \leq 2 \cdot 10^5$ ) — the elements of the final array $c$ that should be obtained from the initial array $a$ .
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ .
The first line of each test case contains a single integer $n$ ( $1 \leq n \leq 2 \cdot 10^5$ ) — the number of elements the final array $c$ should have.
The second line of each test case contains $n$ space-separated integers $c_i$ ( $1 \leq c_i \leq 2 \cdot 10^5$ ) — the elements of the final array $c$ that should be obtained from the initial array $a$ .
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ .
输出格式
For each test case, output "YES" (without quotes) if such a sequence of operations exists, and "NO" (without quotes) otherwise.
You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
输入输出样例
输入 #1
6 1 1 1 2 5 5 1 3 2 1 5 7 1 5 2 1 3 1 1 1 5 1 1 4 2 1
输出 #1
YES NO YES NO YES YES
For the first test case, the initial array $a$ is already equal to $[1]$ , so the answer is "YES".
For the second test case, performing any amount of operations will change $a$ to an array of size at least two which doesn't only have the element $2$ , thus obtaining the array $[2]$ is impossible and the answer is "NO".
For the third test case, we can perform the following operations in order to obtain the final given array $c$ :
- Initially, $a = [1]$ .
- By choosing the subsequence $[1]$ , and inserting $1$ in the array, $a$ changes to $[1, 1]$ .
- By choosing the subsequence $[1, 1]$ , and inserting $1+1=2$ in the middle of the array, $a$ changes to $[1, 2, 1]$ .
- By choosing the subsequence $[1, 2]$ , and inserting $1+2=3$ after the first $1$ of the array, $a$ changes to $[1, 3, 2, 1]$ .
- By choosing the subsequence $[1, 3, 1]$ and inserting $1+3+1=5$ at the beginning of the array, $a$ changes to $[5, 1, 3, 2, 1]$ (which is the array we needed to obtain).
For the second test case, performing any amount of operations will change $a$ to an array of size at least two which doesn't only have the element $2$ , thus obtaining the array $[2]$ is impossible and the answer is "NO".
For the third test case, we can perform the following operations in order to obtain the final given array $c$ :
- Initially, $a = [1]$ .
- By choosing the subsequence $[1]$ , and inserting $1$ in the array, $a$ changes to $[1, 1]$ .
- By choosing the subsequence $[1, 1]$ , and inserting $1+1=2$ in the middle of the array, $a$ changes to $[1, 2, 1]$ .
- By choosing the subsequence $[1, 2]$ , and inserting $1+2=3$ after the first $1$ of the array, $a$ changes to $[1, 3, 2, 1]$ .
- By choosing the subsequence $[1, 3, 1]$ and inserting $1+3+1=5$ at the beginning of the array, $a$ changes to $[5, 1, 3, 2, 1]$ (which is the array we needed to obtain).
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted