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