CodeTON Round 7 补题(C、D)

发布时间 2023-11-26 21:15:46作者: jvdyvan

CodeTON Round 7

C. Matching Arrays

思路

开一个c数组来记录a的从小到大排序后的原来的下标,接着将b数组从小到大排序,先找出将a数组后x个数和b数组x的数比较,再将a的前n - x和b的后n-x个数比较。如果a数组后x个数都大于b数组前x的数,且a的前n - x都不大于b的后n-x个数,则输出YES

ac代码

#include <bits/stdc++.h>

using namespace std;
using i64  = long long;
const i64 inf = 8e18;
typedef pair<int, int> pii;

const int N = 3e5 + 10;
int a[N], b[N], c[N], ans[N];

void solve() {
    int n, x;
    cin >> n >> x;
    for (int i = 0; i < n; i ++) cin >> a[i];
    for (int i = 0; i < n; i ++) cin >> b[i];

    iota(c, c + n, 0);
    sort(c, c + n, [](int x, int y){
        return a[x] < a[y];
    });
    sort(b, b + n);

    bool ok = 1;
    for (int i = n - x; i < n; i++) {
        int idx = c[i];
        if (a[idx] <= b[i - (n - x)]) ok = 0;
        ans[idx] = b[i - (n - x)];
    }

    for (int i = 0; i < n - x; i++) {
        int idx = c[i];
        if (a[idx] > b[x + i]) ok = 0;
        ans[idx] = b[x + i];
    }

    if (ok) {
        cout << "YES\n";
        for (int i = 0; i < n; i++) cout << ans[i] << ' ';
        cout << endl;
    }else cout << "NO\n";
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);

    int t = 1;
    cin >> t;
    while (t --) solve();

    return 0;
}

D. Ones and Twos

思路

用一个set存下数组里为1的下标,用sum记录数组当前的和,用cnt的记录min(前缀2的个数,后缀2的个数)。
对于待查找数s,如果满足s <= t or (s % 2 == t % 2 && (s - t) / 2 <= cnt)则输出YES,否则输出NO

ac代码

#include <bits/stdc++.h>

using namespace std;
using i64  = long long;
const i64 inf = 8e18;
typedef pair<int, int> pii;
const int N = 3e5 + 10;

void solve() {
    int n, q;
    cin >> n >> q;
    set<int> se;
    vector<int> a(n + 1);
    int sum = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        if (a[i] == 1) se.insert(i);
        sum += a[i];
    }

    while (q --) {
        int op; cin >> op;

        if (op == 1) {
            int s; cin >> s;
            if (!se.size()) cout << (s % 2 == 1? "NO\n" : "YES\n");
            else {
                int cnt = min(*se.begin() - 1, n - *se.rbegin());
                int t = sum - 2 * cnt;
                if (s <= t || (s % 2 == t % 2 && (s - t) / 2 <= cnt)) cout << "YES\n";
                else cout << "NO\n";
            }

        }else{
            int i, v;
            cin >> i >> v;
            if (v == 1) se.insert(i);
            else se.erase(i);
            sum += a[i] - v;
            a[i] = v;
        }
    }

}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);

    int t = 1;
    cin >> t;
    while (t --) solve();

    return 0;
}