【Codeforces Round 859 (Div. 4)】G2. Subsequence Addition (Hard Version)题解

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

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

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

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

Input

The first line of the input contains an integer t t t ( 1 ≤ t ≤ 1000 1 \leq t \leq 1000 1t1000) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \leq n \leq 2 \cdot 10^5 1n2105) — the number of elements the final array c c c should have.

The second line of each test case contains n n n space-separated integers c i c_i ci ( 1 ≤ c i ≤ 2 ⋅ 1 0 5 1 \leq c_i \leq 2 \cdot 10^5 1ci2105) — the elements of the final array c c c that should be obtained from the initial array a a a.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

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).
Example
input

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

output

YES
NO
YES
NO
YES
YES

Note

For the first test case, the initial array a a a is already equal to [ 1 ] [1] [1], so the answer is “YES”.

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

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

Topic to the effect
insert image description here

answer

One
<<<<<Thoughts

Let's first talk about the orthodox problem solution, using the method of mathematical conclusions, we know that this number starts from 1, and the special judgment is 1, and the subsequent numbers are all part of the previous number. We only know that it has at least two 1s and one 2 , 1, 2, 3, 4 can be represented, and it is not easy to represent 8 when we take the largest value. . . Mathematical induction finally shows that the maximum value in this array can always be established as long as it does not exceed the sum

<<<<<<代码

    #include <iostream>
    #include <algorithm> 
    using namespace std ;
     
    const int N = 200010 ;
     
    void work() 
    {
    
    	int q[N] ;
    	int n ;
    	cin >> n ;
    	for(int i = 0 ; i < n ; i ++ )
    		cin >> q[i] ;
    	sort (q, q + n) ;
    	if(q[0] != 1) 
    	{
    
    
    		cout << "NO" << endl ;
    		return ;
    	}
    	long long  sum = 1 ;
    	for(int i = 1 ; i < n ; i ++  )
    	{
    
    
     
    		if(sum < q[i])
    		{
    
    
    			
    			cout <<"NO" << endl ;
    			return ;
    		}
    		sum = sum + q[i] ;
    	}
    	cout << "YES" <<endl ;
    	return ;
    	
     
    }
     
    int main ()
    {
    
    
    	int T ;
    	cin >> T ;
    	while ( T -- )
    	{
    
    
    		work() ;
    	}
    	return 0 ;
    }

<<<<< The above idea is the positive solution to the problem, but here is another idea that can solve similar problems, but the idea is different and the time is longer.
2
The solution to this problem is designed to the data structure bitset , please see for details. But this is not the correct answer. I know TLE in G2, but if I understand this data structure, I can directly A G1 when the rules are not summarized.

#include <iostream>
#include <bitset>
#include <algorithm>
using namespace std ;
const int N = 6000 ; 

bitset <N> b ;
int q[N] ;
void work() 
{
    
    
	int n ;
	cin >> n ;
	for(int i = 0 ; i < n ;  i++ )
	{
    
    
		cin >> q[i] ;
	}	
	sort(q , q + n ) ;
	if( q[0] != 1 )
	{
    
    
		cout << "NO" << endl ;
		return ;
	}
	int sum = 1 ;
	for(int i = 1 ; i < n ; i ++ )
	{
    
    
		if(sum < q[i])
		{
    
    
			cout << "NO" << endl ;
			return ;
		}
		sum += q[i] ;
	}
	cout << "YES" << endl ;
	return ;

}

int main () 
{
    
    
	int n ;
	cin >>  n ;
	while ( n -- )
	{
    
    
		work() ;
	}
	return 0 ;
}

Guess you like

Origin blog.csdn.net/wen030803/article/details/131989404