博主头像
mxd's Blog

以技术为翼,以生活为魂

CSP-J 2026 第二轮 个人做题记录 题解?

洛谷民间自测数据 100分

Luogu 数据
Luogu 数据

[CSP-J 2026] 旅游 / travel

#include <bits/stdc++.h>
using namespace std;

int side(char ch) {
    if (ch == 'A' || ch == 'B' || ch == 'C') return 0;
    return 1;
}

int pos(char ch) {
    if (ch == 'A') return 0;
    if (ch == 'B') return 1;
    if (ch == 'C') return 2;
    if (ch == 'D') return 0;
    return 1;
}

int main() {
    freopen("travel.in", "r", stdin);
    freopen("travel.out", "w", stdout);

    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string c1, s1, c2, s2;
    cin >> c1 >> s1 >> c2 >> s2;

    bool sameCar = (c1 == c2);
    bool sameRow = sameCar && (s1.substr(0, 2) == s2.substr(0, 2));

    char a = s1[2], b = s2[2];

    bool sameSide = sameRow && (side(a) == side(b));

    bool adjacent = sameSide && (abs(pos(a) - pos(b)) == 1);

    cout << sameCar << sameRow << sameSide << adjacent << '\n';

    return 0;
}

[CSP-J 2026] 回文串 / string

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main() {
    freopen("string.in", "r", stdin);
    freopen("string.out", "w", stdout);

    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    vector<ll> a(n);
    vector<char> c(n);

    for (int i = 0; i < n; ++i) {
        cin >> a[i] >> c[i];
    }

    int l = 0, r = n - 1;
    ll remL = a[l], remR = a[r];
    ll ans = 0;

    while (l < r) {
        ll t = min(remL, remR);

        if (c[l] != c[r]) {
            ans += t;
        }

        remL -= t;
        remR -= t;

        if (remL == 0) {
            ++l;
            if (l <= r) remL = a[l];
        }

        if (remR == 0) {
            --r;
            if (r >= l) remR = a[r];
        }
    }

    cout << ans << '\n';
    return 0;
}

[CSP-J 2026] 按位与 / and

#include <bits/stdc++.h>
using namespace std;

int main() {
    freopen("and.in", "r", stdin);
    freopen("and.out", "w", stdout);

    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;

    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    int ans = 0;

    for (int b = 29; b >= 0; --b) {
        int now = ans | (1 << b);

        int cnt = 0;
        for (int x : a) {
            if ((x & now) == now) {
                ++cnt;
            }
        }

        if (cnt >= m) {
            ans = now;
        }
    }

    cout << ans << '\n';

    return 0;
}

[CSP-J 2026] 纸牌游戏 / card

#include <bits/stdc++.h>
using namespace std;

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

    freopen("card.in", "r", stdin);
    freopen("card.out", "w", stdout);

    int n, m;
    cin >> n >> m;

    vector<int> a(n + 1);
    vector<int> last(m + 1, 0);

    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        last[a[i]] = i;
    }

    vector<int> nxt(n + 2);

    for (int i = 1; i <= n; ++i) {
        if (last[a[i]] > i) {
            nxt[i] = last[a[i]] + 1;
        } else {
            nxt[i] = i + 1;
        }
    }

    vector<long long> sz(n + 2, 1);
    vector<long long> ans(m + 1, 0);

    // 反向树上计算子树大小
    for (int i = 1; i <= n; ++i) {
        sz[nxt[i]] += sz[i];
    }

    for (int i = 1; i <= n; ++i) {
        ans[a[i]] += sz[i];
    }

    for (int i = 1; i <= m; ++i) {
        if (i > 1) cout << ' ';
        cout << ans[i];
    }
    cout << '\n';

    return 0;
}
CSP-J 2026 第二轮 个人做题记录 题解?
https://blog.mxdyeah.com/post/csp-j-2026-round-2
本文作者 mxdyeah
发布时间 2026-10-31
许可协议 CC BY-NC-SA 4.0
发表新评论