2024 信友队 CSP-J 第二轮(复赛)模拟赛

A 火柴

#include <cstdio>
int cnt[10] = {0, 1, 2, 3, 3, 2, 3, 4, 5, 3};
char num[10][10] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
int main()
{
    freopen("match.in", "r", stdin);
    freopen("match.out", "w", stdout);
    int n; scanf("%d", &n);
    int ans = 0;
    for (int i = 1; i <= 39; i++) {
        int x = i / 10, y = i % 10;
        if (x * 2 + cnt[y] == n) ans++;
    }
    printf("%d\n", ans);
    for (int i = 1; i <= 39; i++) {
        int x = i / 10, y = i % 10;
        if (x * 2 + cnt[y] == n) {
            for (int j = 1; j <= x; j++) printf("X");
            printf("%s\n", num[y]);
        }
    }
    return 0;
}

B 失误

#include <cstdio>
#include <algorithm>
using std::min;
using std::max;
using ll = long long;
const int N = 100005;
const ll INF = 1e18;
ll a[N], pre[N], suf[N];
int main()
{
    freopen("mistake.in", "r", stdin);
    freopen("mistake.out", "w", stdout);
    int n; scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%lld", &a[i]);
        pre[i] = pre[i - 1] ^ a[i];
    }
    for (int i = n; i >= 1; i--) {
        suf[i] = suf[i + 1] ^ a[i];
    }
    ll ans_min = INF, ans_max = 0;
    for (int i = 2; i <= n; i++) {
        ll cur = (pre[i - 1] + a[i]) ^ suf[i + 1];
        ans_min = min(ans_min, cur);
        ans_max = max(ans_max, cur);
    }
    printf("%lld %lld\n", ans_min, ans_max);
    return 0;
}

D 投票

#include <cstdio>
#include <vector>
#include <algorithm>
using std::vector;
using std::sort;
using ll = long long; 
const int N = 100005;
vector<int> tree[N];
int p[N], a[N], op[N], b[N];
ll ans[N], rev[N];
void dfs(int u) {
    vector<int> vec1, vec0;
    for (int v : tree[u]) {
        dfs(v);
        if (op[v]) vec1.push_back(v);
        else vec0.push_back(v);
    }

    if (vec1.size() == vec0.size()) {
        ans[u] = 0; op[u] = a[u];
    } else if (vec1.size() > vec0.size()) {
        op[u] = 1;
        int cnt = (vec1.size() - vec0.size()) / 2;
        sort(vec1.begin(), vec1.end(), [](int lhs, int rhs) {
            return rev[lhs] < rev[rhs];
        }); 
        for (int i = 0; i < cnt; i++) ans[u] += rev[vec1[i]];
    } else {
        op[u] = 0;
        int cnt = (vec0.size() - vec1.size()) / 2;
        sort(vec0.begin(), vec0.end(), [](int lhs, int rhs) {
            return rev[lhs] < rev[rhs];
        });
        for (int i = 0; i < cnt; i++) ans[u] += rev[vec0[i]];
    }

    if (a[u]) vec1.push_back(u);
    else vec0.push_back(u);
    rev[u] = b[u];
    ll tmp = 0;
    if (vec1.size() > vec0.size()) {
        int cnt = (vec1.size() - vec0.size() + 1) / 2;
        sort(vec1.begin(), vec1.end(), [](int lhs, int rhs) {
            return rev[lhs] < rev[rhs];
        });
        for (int i = 0; i < cnt; i++) tmp += rev[vec1[i]];
    } else {
        int cnt = (vec0.size() - vec1.size() + 1) / 2;
        sort(vec0.begin(), vec0.end(), [](int lhs, int rhs) {
            return rev[lhs] < rev[rhs];
        });
        for (int i = 0; i < cnt; i++) tmp += rev[vec0[i]];
    }
    rev[u] = tmp;
    
}
int main()
{

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

    int n; scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d%d%d", &p[i], &a[i], &b[i]);
        tree[p[i]].push_back(i);
    }
    dfs(1);
    for (int i = 1; i <= n; i++) {
        printf("%lld\n", ans[i]);
    }
    return 0;
}
posted @ 2024-10-22 18:05  RonChen  阅读(103)  评论(0编辑  收藏  举报