首页 > 其他分享 >Subsequence Addition

Subsequence Addition

时间:2023-07-22 10:33:50浏览次数:41  
标签:case Addition subsequence leq Subsequence test array ##

# Subsequence Addition (Hard Version)

## 题面翻译

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

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

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

有 $t$ 组数据。

$1\leq n\leq2\times10^5,1\leq c_i\leq2\times10^5,1\leq t\leq1000$

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 $ ^{\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 $ .

## 输出格式

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

 

标签:case,Addition,subsequence,leq,Subsequence,test,array,##
From: https://www.cnblogs.com/o-Sakurajimamai-o/p/17572942.html

相关文章

  • CF1132G Greedy Subsequences
    简单题。\(i\)向\(i\)后第一个\(j\),\(a_j\)比\(a_i\)大的点连边,不难发现最后形成了一棵森林,并且一个点的父亲\(\text{fa}_i>i\)。题目变成了取\([l,r]\)中的点为起点,向祖先方向走去并且终点编号\(\ler\)的最长链长度。考虑离线,维护从每个点开始的最长链长度\(f_i......
  • [LeetCode] 2486. Append Characters to String to Make Subsequence
    Youaregiventwostrings s and t consistingofonlylowercaseEnglishletters.Return theminimumnumberofcharactersthatneedtobeappendedtotheendof s sothat t becomesa subsequence of s.A subsequence isastringthatcanbederived......
  • hdu 2227 Find the nondecreasing subsequences (树状数组+dp+离散化)
    题意:给定一个长度为n(n<=100000)的整数序列,求其中的递增序列的个数。对于某些序列中的递增序列的个数是可以用dp来求解的,其状态转移方程为:dp[i]=sum(dp[j])+1,j<i&&a[j]<a[i]根据状态转移方程可以得知这样dp的时间复杂度为O(n^2),而对于题目给定的10^6的数量级来说,这样......
  • vue: number addition
     单页应用:(SinglePageApp,SPA)体现了其强大的优势。页面是局部刷新的,响应速度快,不需要每次加载所有的CSS/JS。前后端分离,前端(手机端)不受后端(服务器端)的开发语言的限制。Angular,React,Vue.js框架都是很好的选择。https://github.com/vuejs/awesome-vue <!DOCTYPEhtml><......
  • PAT-甲级-1007 Maximum Subsequence Sum C++
    Givenasequenceof K integers{ N1​, N2​,..., N​K }.Acontinuoussubsequenceisdefinedtobe{ Ni​, Ni+1​,..., Nj​ }where 1≤i≤j≤K.TheMaximumSubsequenceisthecontinuoussubsequencewhichhasthelargestsumofitselements.Fore......
  • [LeetCode] 2542. Maximum Subsequence Score
    Youaregiventwo 0-indexed integerarrays nums1 and nums2 ofequallength n andapositiveinteger k.Youmustchoosea subsequence ofindicesfrom nums1 oflength k.Forchosenindices i0, i1,..., ik-1,your score isdefinedas:Thes......
  • Atcoder ARC159C Permutation Addition
    设\(s=\sum\limits_{i=1}^na_i\),考虑加上排列\(p\),那么\(s\leftarrows+\frac{n\times(n+1)}{2}\)。当有解时,因为\(a_i\)相等,所以有\(s\bmodn=0\)。考虑\(n\bmod2=1\)时,\(\frac{n\times(n+1)}{2}\bmodn=0\),所以\(s\bmodn=0\)时才会有解。......
  • 【CF1621G】Weighted Increasing Subsequences 题解(优化树状数组)
    CF传送门|LG传送门。优化树状数组+反向处理。Solution发现直接做不好下手。难点主要在求出所有的上升子序列并计算它们分别的贡献。所以需要反向考虑每个单点在什么情况下产生贡献。一个单点会产生多少贡献。一个单点产生贡献的条件很容易得到。一个是在一个上升子序......
  • Scoring Subsequences
    ScoringSubsequencestimelimitpertest2.5secondsmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputThe score ofasequence [s1,s2,…,sd][1,2,…,] isdefinedas s1⋅s2⋅…⋅sdd!1⋅2⋅…⋅!,where d!=1⋅2⋅…⋅d!=1......
  • 「解题报告」CF1621G Weighted Increasing Subsequences
    比较套路的拆贡献题。考虑直接枚举那个\(j\),求有多少包含\(j\)的上升子序列满足这个子序列最后一个数的后面有大于\(a_j\)的数。首先对于\(j\)前面的选择方案是没有影响的,可以直接拿树状数组DP一遍得到。后面的过程我们可以找到从后往前第一个大于\(a_j\)的数的位置......