Subsequence Addition

# Subsequence Addition (Hard Version)

## 题面翻译

本题为困难版,两题的唯一区别在于数据范围的大小。

数列 a 最开始只有一个数 1,你可以进行若干次操作,每次操作你可以选取 k 个数(k 无限制,小于等于 a 的大小即可),将这 k 个数的和放入 a 的任意一个位置。

给定一个长度为 n 的序列 c,问 a 能否在进行若干次操作后转为 c

t 组数据。

1n2×105,1ci2×105,1t1000

by @[Larryyu](https://www.luogu.com.cn/user/475329)

## 题目描述

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

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 ( 1k|a| ) distinct indices i1,i2,,ik and insert anywhere into a a new element with the value equal to ai1+ai2++aik .

## 输入格式

The first line of the input contains an integer t ( 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 ( 1n2105 ) — the number of elements the final array c should have.

The second line of each test case contains n space-separated integers ci ( 1ci2105 ) — 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 2105 .

## 输出格式

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

## 样例 #1

### 样例输入 #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).

复制代码
//Subsequence Addition //只需要新添加的数小于前几个数的前缀和就可以了 #include <bits/stdc++.h> #define int long long using namespace std; const int N=1e6+10,mod=1e9+7; int s[N]; int n,t,a[N],f[N],res,num,ans,m; bool vis[N]; signed main() { std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); cin>>t; while(t--){ cin>>n; bool f=false; for(int i=1;i<=n;i++) cin>>a[i]; sort(a+1,a+n+1); for(int i=1;i<=n;i++) s[i]=s[i-1]+a[i]; if(s[1]!=1) f=true; for(int i=2;i<=n;i++) if(a[i]>s[i-1]) f=true; if(f) cout<<"NO"<<endl; else cout<<"YES"<<endl; } return 0; }
复制代码

 


__EOF__

本文作者Sakurajimamai
本文链接https://www.cnblogs.com/o-Sakurajimamai-o/p/17572942.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   o-Sakurajimamai-o  阅读(22)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
-- --
点击右上角即可分享
微信分享提示