CF1807G2.Subsequence Addition (Hard Version)

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

The only difference between the two versions is that in this version, the constraints are higher.

Initially, array aa contains just the number 11. You can perform several operations in order to change the array. In an operation, you can select some subsequence†^{\dagger} of aa and add into aa an element equal to the sum of all elements of the subsequence.

You are given a final array cc. Check if cc can be obtained from the initial array aa by performing some number (possibly 0) of operations on the initial array.

†^{\dagger} A sequence bb is a subsequence of a sequence aa if bb can be obtained from aa by the deletion of several (possibly zero, but not all) elements. In other words, select kk (1≤k≤∣a∣1 \leq k \leq |a|) distinct indices i1,i2,…,iki_1, i_2, \dots, i_k and insert anywhere into aa a new element with the value equal to ai1+ai2+⋯+aika_{i_1} + a_{i_2} + \dots + a_{i_k}.

两个版本的唯一区别在于,本版本的约束条件更高。

初始时,数组 aa 仅包含数字 11。你可以执行若干次操作来改变该数组。每次操作中,你可以选择 aa 的某个子序列†^{\dagger},并将该子序列所有元素之和作为一个新元素添加到 aa 中。

现给定一个最终数组 cc。请判断:是否存在一系列(可能为零次)操作,使得从初始数组 aa 出发能够得到 cc。

†^{\dagger} 序列 bb 是序列 aa 的一个子序列,当且仅当 bb 可通过从 aa 中删除若干(可能为零个,但不能全部)元素而得到。换言之,选取 kk 个(1≤k≤∣a∣1 \leq k \leq |a|)互不相同的下标 i1,i2,…,iki_1, i_2, \dots, i_k,并在 aa 中任意位置插入一个值为 ai1+ai2+⋯+aika_{i_1} + a_{i_2} + \dots + a_{i_k} 的新元素。

输入格式

The first line of the input contains an integer tt (1≤t≤10001 \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 nn (1≤n≤2⋅1051 \leq n \leq 2 \cdot 10^5) — the number of elements the final array cc should have.

The second line of each test case contains nn space-separated integers cic_i (1≤ci≤2⋅1051 \leq c_i \leq 2 \cdot 10^5) — the elements of the final array cc that should be obtained from the initial array aa.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

输入的第一行包含一个整数 tt(1≤t≤10001 \leq t \leq 1000),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \leq n \leq 2 \cdot 10^5),表示最终数组 cc 应包含的元素个数。

每个测试用例的第二行包含 nn 个以空格分隔的整数 cic_i(1≤ci≤2⋅1051 \leq c_i \leq 2 \cdot 10^5),即应由初始数组 aa 得到的最终数组 cc 的各个元素。

保证所有测试用例的 nn 值之和不超过 2⋅1052 \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).

对于每个测试用例,如果存在这样的一系列操作,则输出 "YES"(不带引号);否则输出 "NO"(不带引号)。

您可以以任意大小写形式输出答案(例如,字符串 "yEs"、"yes"、"Yes" 和 "YES" 均被视为肯定回答)。

输入输出样例

  • 输入#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 aa is already equal to [1][1], so the answer is "YES".

For the second test case, performing any amount of operations will change aa to an array of size at least two which doesn't only have the element 22, thus obtaining the array [2][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 cc:

  • Initially, a=[1]a = [1].
  • By choosing the subsequence [1][1], and inserting 11 in the array, aa changes to [1,1][1, 1].
  • By choosing the subsequence [1,1][1, 1], and inserting 1+1=21+1=2 in the middle of the array, aa changes to [1,2,1][1, 2, 1].
  • By choosing the subsequence [1,2][1, 2], and inserting 1+2=31+2=3 after the first 11 of the array, aa changes to [1,3,2,1][1, 3, 2, 1].
  • By choosing the subsequence [1,3,1][1, 3, 1] and inserting 1+3+1=51+3+1=5 at the beginning of the array, aa changes to [5,1,3,2,1][5, 1, 3, 2, 1] (which is the array we needed to obtain).

对于第一个测试用例,初始数组 aa 已经等于 [1][1],因此答案为 “YES”。

对于第二个测试用例,执行任意次数的操作都会使 aa 变为长度至少为 2 的数组,且该数组不可能仅包含元素 22,因此无法得到数组 [2][2],答案为 “NO”。

对于第三个测试用例,我们可以按以下顺序执行操作,以获得最终给定的数组 cc:

  • 初始时,a=[1]a = [1]。
  • 选择子序列 [1][1],并在数组中插入 11,aa 变为 [1,1][1, 1]。
  • 选择子序列 [1,1][1, 1],并在数组中间插入 1+1=21+1=2,aa 变为 [1,2,1][1, 2, 1]。
  • 选择子序列 [1,2][1, 2],并在数组第一个 11 之后插入 1+2=31+2=3,aa 变为 [1,3,2,1][1, 3, 2, 1]。
  • 选择子序列 [1,3,1][1, 3, 1],并在数组开头插入 1+3+1=51+3+1=5,aa 变为 [5,1,3,2,1][5, 1, 3, 2, 1](即我们需要得到的数组)。

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

首页