G2. Subsequence Addition (Hard Version)【二进制拆分】

Codeforces Round 859 (Div. 4)

题意

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

解法

这里顺便把G1写了,因为任何一个数的二进制数都可以表示成2^i + 2 ^ j + 2 ^ q … 所以,这里根据题目进行二进制拆分。题目最开始给的是1,则之后的数肯定是1, 1那么,则可以表示出取值范围为前面所以数之和。只需要排序之后,判断前面的数之和与现在数的大小就可以了。

#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;
const int maxn = 2E5 + 7;
int a[maxn];
int main()
{
    int x;
    cin >> x;
    while (x --)
    {

    int n;
    cin >> n;

    for (int i = 1; i <= n; i ++)
        cin >> a[i];

    sort(a + 1, a + 1 + n);

    long long now = 1;
    bool flag = true;

    if (n == 1 && a[1] != 1)
    {
        cout << "NO" << '\n';
        continue;
    }
    for (int i = 2; i <= n; i ++)
    {
        if (a[i] > now)
        {
            flag = false;
            break;
        }
        now += a[i];
    }
    if (flag)
        cout << "YES" << '\n';
    else 
        cout << "NO" << '\n';
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值