CF1927C 题解

· · 题解

思路

也是一个贪心题。可以枚举 1\sim k 的所有数,若此数在 a 和 b 中都未出现过,则一定无解了;若只在 a 中出现而不在 b 中出现,则说明不能用 b 里的数字,totb 加 1,只在 b 中出现同理。结束时,如果 totb>\frac k2 或 tota>\frac k2,则说明无解了。

代码

# include <bits/stdc++.h>
using namespace std;
int t, n, m, k, a[200005], b[200005], i, j, tot1, tot2;
int main () {
    ios::sync_with_stdio (0);
    cin.tie (0);
    cout.tie (0);
    cin >> t;
    while (t --) {
        cin >> n >> m >> k;
        for (int i = 0; i < n; ++ i)
            cin >> a[i];
        for (int i = 0; i < m; ++ i)
            cin >> b[i];
        sort (a, a + n), sort (b, b + m);
        n = unique (a, a + n) - a, m = unique (b, b + m) - b;
        a[n] = b[m] = i = j = tot1 = tot2 = 0;
        for (int x = 1; x <= k; ++ x)
            if (a[i] != x)
                if (b[j] != x) {
                    cout << "NO\n";
                    goto there;
                } else
                    ++ j, ++ tot1;
            else if (b[j] != x)
                ++ i, ++ tot2;
            else
                ++ i, ++ j;
        k >>= 1;
        if (tot1 > k || tot2 > k)
            cout << "NO\n";
        else
            cout << "YES\n";
there:
        ;
    }
    return 0;
}