【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).

题目大意
在这里插入图片描述

题解


<<<<<思路

我们首先说正统的题解,使用数学结论的方法,我们知道这个数从1开始,特判1 ,后面的数都是前面的数的一部分的和,我们只知道他至少有两个1 和 一个2 ,1,2,3,4可以表示,当我们取最大的时候最不容易表示8 。。。数学归纳最后可知这个数组中的最大值只要不超过和就能一直成立

<<<<<<代码

    #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 ;
    }

<<<<< 以上思路为题目的正解,但是这里提供一种其他的思路也能解决类似问题,但是思路不一样,时间更长。

本题解设计到数据结构 bitset,详情请看。但这个并不是正确的答案,在G2 会TLE ,但是了解这种数据结构,在没有归纳出来规律的时候可以直接A G1

#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 ;
}

猜你喜欢

转载自blog.csdn.net/wen030803/article/details/131989404