【构造】D - Game With Array

D - Game With Array

题意:构造一个长度为\(N\),和为\(S\)的正整数序列,问能不能找到一个1~S的数\(K\),使得数组里找不出一个子序列的和为\(K\)或者\(S-K\)

思路:构造一个前\(N-1\)项均为1,第\(N\)项为\(S-(N-1)\)的数组即可。此时只要\(S-(N-1)>N\),取\(K=N\)就一定能满足条件。

(前\(N-1\)项的任意子序列之和必不为\(N\);且当\(S-N>N-1\),也就是\(S-(N-1)>N\)时,前\(N-1\)项的任意子序列之和也必不为\(S-N\)

void solve() {
    int n, s;
    cin >> n >> s;
    if (s - n + 1 > n)
    {
        cout << "YES" << endl;
        for (int i = 1; i < n; i++)
            cout << 1 << ' ';
        cout << s - n + 1 << endl;
        cout << n << endl;
    }
    else
        cout << "NO" << endl;
}

猜你喜欢

转载自www.cnblogs.com/streamazure/p/12907971.html