B. Good Arrays

isomer / 2024-04-23 / 原文

B. Good Arrays

原本我的思路是找为1的数sum1,如果sum1 > n - sum1, 那就不能让每个为1的数+1, 不为1的数-1构造新的数组
有一个老是出错(关我什么事啊)
我是这样理解题解的那个判断:
假设新数组每个数都是1,再加上原数组为1的个数,这样保证在原数组为1的位置在新数组的数就是2了
如果加上以后小于等于总和,我就是可以选择加与不加构造新的数组
(codeforce题都是很巧的方法,考脑子)

#include <iostream>
using namespace std;


void solve()
{
    long long n;
    cin >> n;
    int ans1 = 0;
    long long sum = 0;
    for (int i = 0; i < n; i ++ )
    {
        int x;
        cin >> x;
        if (x == 1) ans1 ++;
        sum += x;
    }
    if (sum >= ans1 + n && n > 1)
    {
        cout << "YES" << endl;
    }
    else
    {
        cout << "NO" << endl;
    }
}

int main()
{
    int t;
    cin >> t;
    while (t -- )
    {
        solve();
    }
    return 0;
}