比赛记录

· · 个人记录

或许还是放不下算法竞赛。可能以后会和 wx 和 xhl 老师一起打 ACM。

最近可能要进入郁期了不是很有动力。补题以后再说。

[SCCPC 2026] 2026 年四川省大学生程序设计竞赛重现赛

4/12

点击即可查看

D

按奇偶分讨。对偶数,奇数位的字母出现次数和偶数位应当一一对应;对奇数,和中心奇偶性相同的位置可以且仅可以有一个字母出现次数为奇数。

#include <bits/stdc++.h>

const int MAX = 105;

int T;
int a[MAX], b[MAX];
char s[MAX];

void solve() {
    scanf("%s", s);
    int n = strlen(s);
    for (int i = 1; i <= 26; ++i) { a[i] = b[i] = 0; }
    if (n % 2 == 0) {
        for (int i = 0; i < n; ++i) {
            if (i % 2 == 0) { ++a[s[i] - 'a' + 1]; }
            else            { --a[s[i] - 'a' + 1]; }
        }
        for (int i = 1; i <= 26; ++i) { 
            if (a[i] != 0) { printf("NO\n"); return ; }
        }
        printf("YES\n"); return ;
    } else {
        for (int i = 0; i < n; ++i) {
            if (i % 2 == 0) { ++a[s[i] - 'a' + 1]; }
            else            { ++b[s[i] - 'a' + 1]; }
        }
        int cnt = 0;
        if ((n / 2) % 2 == 0) {
            for (int i = 1; i <= 26; ++i) { if (b[i] % 2) { printf("NO\n"); return ; } }
            for (int i = 1; i <= 26; ++i) { if (a[i] % 2) { ++cnt; } }
            if (cnt <= 1) { printf("YES\n"); return ; } else { printf("NO\n"); return ; }
        } else {
            for (int i = 1; i <= 26; ++i) { if (a[i] % 2) { printf("NO\n"); return ; } }
            for (int i = 1; i <= 26; ++i) { if (b[i] % 2) { ++cnt; } }
            if (cnt <= 1) { printf("YES\n"); return ; } else { printf("NO\n"); return ; }
        }
    }
}

int main() {
    scanf ("%d", &T); while (T--) { solve(); }
}

H

考虑图的结构。为方便理解,我们把“有边”的条件翻译为:若二进制下有奇数个位置不同,则有边。

则由奇偶性,

因此任意选择一个点 u ,其余的点分为与 u 相连和不相连两类,且两类内部无边。只需要选择一类即可。

#include <bits/stdc++.h>

typedef long long ll;
const int MAX = 1e6 + 5;

int T, n;
int a[MAX];

int popcnt(int x) {
    int cnt = 0;
    while (x) { x &= (x - 1); ++cnt; }
    return cnt;
}

void solve() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); }
    ll x = a[1], y = 0;
    for (int i = 2; i <= n; ++i) {
        if (popcnt(a[1] ^ a[i]) % 2 == 1) { y += a[i]; }
        else { x += a[i]; }
    }
    printf("%lld\n", std::max(x, y));
}

int main() {  
    scanf("%d", &T); while (T--) { solve(); }
}

G

维护前缀和后缀,枚举前缀查询即可。

#include <bits/stdc++.h>

typedef long long ll;

const ll MAX = 1e6 + 15;
ll n, ans, T, k;
ll a[MAX], p[MAX], s[MAX];

std::unordered_map<ll, std::vector<ll>> mp;

ll cnt(ll x, ll v) {
    auto it = std::lower_bound(mp[v].begin(), mp[v].end(), x);
    return std::distance(it, mp[v].end());
}

void solve() {
    ans = 0; mp.clear();
    scanf("%lld%lld", &n, &k);
    for (int i = 1; i <= n; ++i) { scanf("%lld", &a[i]); }
    for (int i = 1; i <= n + 5; ++i) { s[i] = p[i] = 0; }
    for (int i = 1; i <= n; ++i) { p[i] = p[i - 1] + (i % 2 == 1 ? 1 : -1) * a[i]; }
    for (int i = n; i >= 1; --i) { s[i] = a[i] - s[i + 1]; }
    for (int i = 1; i <  n; ++i) { if (p[i] == k) { ++ans; } }
    for (int i = 2; i <= n; ++i) { if (s[i] == k) { ++ans; } }
    if (k == 0) { ++ans; }
//  for (int i = 1; i <= n; ++i) { printf("%lld ", p[i]); } printf("\n");
//  for (int i = 1; i <= n; ++i) { printf("%lld ", s[i]); } printf("\n");
    for (int i = 1; i <= n; ++i) { mp[s[i]].push_back(i); }

    for (int i = 1; i <= n; ++i) {
        if ((i + 1) % 2 == 1) { ans += cnt(i + 2,   k - p[i]); }
        else                  { ans += cnt(i + 2, -(k - p[i])); }
    }

    printf("%lld\n", ans);
}

int main() {
    scanf("%lld", &T); while (T--) { solve(); }
}

C

为方便实现添加一个“强制换下当前精灵”的操作,不难发现答案不会更劣。

令 f_{n, k} 是 n 次对战后,剩余精灵为 k 号的最小使用精灵数。k = 0 代表同归于尽。h_{n} = \min_{x}{f_{n, x}}

令 S_k 是克制 k 的精灵集合,a_n 是第 n 个敌方精灵。

对第 n 轮,这轮赢下对方的部分有

\begin{aligned} & \min({f_{i - 1, k}, 1 + h_{i - 1}, 1 + f_{i - 1})}, &if k \in S_{a_{i - 1}} \\ &\min(1 + h_{i - 1}, 1 + f_{i - 1}), &otherwise \end{aligned} \right.

平局的部分一方面是强制平局,f_{n, 0} = \min(f_{n, 0}, 1 + h_{i - 1});一部分是剩余的精灵恰好平局时 \forall k \ s.t.\ k \notin S_{a_{n}}, a_{n} \notin S_k, f_{n, 0} = \min(f_{n, 0}, f_{i - 1, k})

#include <bits/stdc++.h>

const int MAX = 1e5 + 5;
const int INF = 1e9 + 7;

int n, m, k, s, tmp, ans;
int h[MAX], a[MAX];
std::map<int, int> f[MAX];
std::set<int> st[MAX];

int main() {
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &s);
        for (int j = 1; j <= s; ++j) {
            scanf("%d", &tmp); st[i].insert(tmp);
        }
    }
    for (int i = 1; i <= m; ++i) { scanf("%d", &a[i]); }

    for (auto p : st[a[1]]) { f[1][p] = 1; }
    h[1] = 1; f[1][0] = 1;

    for (int i = 2; i <= m; ++i) {
        h[i] = INF;
        for (auto p : st[a[i]]) {
            f[i][p] = INF;
            if (st[a[i - 1]].contains(p)) {
                f[i][p] = f[i - 1][p];
            }
            f[i][p] = std::min(f[i][p], 1 + h[i - 1]);
            f[i][p] = std::min(f[i][p], 1 + f[i - 1][0]);
            h[i] = std::min(h[i], f[i][p]);
        }
        f[i][0] = 1 + h[i - 1];
        h[i] = std::min(h[i], f[i][0]);
        for (auto p : st[a[i - 1]]) {
            if (!st[a[i]].contains(p) and !st[p].contains(a[i])) {
                f[i][0] = std::min(f[i][0], f[i - 1][p]);
                h[i] = std::min(f[i][0], h[i]);
            }
        }
    }

    ans = INF;
    for (auto p : st[a[m]]) { ans = std::min(ans, f[m][p]); }
    ans = std::min(ans, f[m][0]);
    printf("%d", ans);
}

补题

WIP

【MX-S15】梦熊 S 组 · 凡尔赛 & DLESS Round 5(同步赛)

全是暴力就不讲了吧。

A

#include <bits/stdc++.h>

typedef long long ll;

const int MAX = 1e6 + 5;

bool flag;
ll n, k, c, T, mx;
std::vector <ll> a, ans;

void count(std::vector <ll> a) {
    ll s = 0;
    for (int i = 1; i <= n; ++i) { s |= a[i]; }
    if (s >= mx) { mx = s; ans = a; }
}

void search(std::vector <ll> a, ll k) {
    if (!k) { count(a); return ; }
    for (int i = 1; i <= n; ++i) {
        for (int j = 0; j <= 61; ++j) {
            if ((1 << j) > k) { break; }
            auto b = a; b[i] += (1 << j);
            search(b, k - (1 << j));
        }
    }
}

void solve() {
    flag = false; mx = 0; ans.clear();
    scanf("%lld%lld", &n, &k); a.resize(n + 5);
    for (int i = 1; i <= n; ++i) { scanf("%lld", &a[i]); }
    search(a, k);
    printf("%lld\n", mx);

    for (int i = 1; i <= n; ++i) { printf("%lld ", ans[i] - a[i]); }
    printf("\n");
}

const ll INF = (1ll << 62);

void _solve() {
    scanf("%lld%lld", &n, &k); a.resize(n + 5);
    for (int i = 1; i <= n; ++i) { scanf("%lld", &a[i]); } auto b = a;
    for (int i = 61; i >= 0; --i) {
        ll mn = INF, p = 0;
        for (int j = 1; j <= n; ++j) {
            ll x = a[j] & ((1ll << (i + 1)) - 1);
            if ((1ll << i) - x < mn) { mn = (1ll << i) - x; p = j; } 
        }
        if (mn < 0) { continue; }
        if (mn <= k) { k -= mn; a[p] += mn; }
    }
    if (k) {
        ll mx = 0; auto ans = a;
        for (int i = 1; i <= n; ++i) {
            auto c = a;
            c[i] += k;
            ll q = 0; for (int i = 1; i <= n; ++i) { q |= a[i]; }
            if (q >= mx) { mx = q; ans = c; }
        }
        a = ans;
    }
    ll q = 0; for (int i = 1; i <= n; ++i) { q |= a[i]; }
    printf("%lld\n", q);
    for (int i = 1; i <= n; ++i) { printf("%lld ", a[i] - b[i]); }
    printf("\n");
}

int main() {
    scanf("%lld%lld", &c, &T); if (c < 4) { while (T--) { solve(); } } else { while (T--) { _solve(); } }
}

B

#include <bits/stdc++.h>

typedef long long ll;

const int MAX = 1e6 + 5;
const ll MOD = 998244353;
ll n;
std::vector <ll> a;
std::unordered_set<ll> s;

ll hash(std::vector <ll> a) {
    ll h = 0;
    for (int i = 1; i <= n; ++i) {
        h = (h * (n + 1) % MOD + a[i]) % MOD;
    }
    return h;
}
void count(std::vector <ll> a) { s.insert(hash(a)); }

void search(int i, int j, bool flag, std::vector <ll> a) {
    auto b = a;
    int _i = i, _j = j + 1;
    if (_j > n - _i) { _j = 1; ++_i; }
    if (a[j] > a[j + 1]) { std::swap(a[j], a[j + 1]); }
    if (flag) { if (b[j] <= b[j + 1]) { std::swap(b[j], b[j + 1]); } }
    if (_i == n) { if (!flag) { count(a); } else { count(b); } }
    else {
        search(_i, _j, flag, a);
        if (flag) { search(_i, _j, false, b); }
    }
}

int main() {
    scanf("%lld", &n); a.resize(n + 5);
    for (int i = 1; i <= n; ++i) { scanf("%lld", &a[i]); }
    if (n >= 100) {
        printf("%lld", n * (n - 1) / 2);
    } else {
        search(1, 1, true, a);
        printf("%lld", s.size());
    }
}

C

#include<bits/stdc++.h>

typedef long long ll;

const int MAX = 1e6 + 5;

bool flag;
ll a[MAX], k, c, tmp, n;
std::set<ll> s[MAX];


void solve1() {
    ll ans = 0;
    for (auto x : s[1]) {
        for (auto y : s[2]) {
            ll A = std::abs(x - a[1]), B = std::abs(x - a[2]),
               C = std::abs(y - a[1]), D = std::abs(y - a[2]);
            ans = std::max(ans, 2 * std::min(A + D, B + C));
        }
    }

    printf("%lld", ans);
}

int main() {
    scanf("%lld%lld", &n, &k);
    for (int i = 1; i <= n; ++i) { scanf("%lld", &a[i]); }
    for (int i = 1; i <= n; ++i) { scanf("%lld", &c); for (int j = 1; j <= c; ++j) { scanf("%lld", &tmp); s[i].insert(tmp); } }
    if (n == 2) { solve1(); } 
}

D

#include <bits/stdc++.h>

typedef long long ll;

const ll MAX = 1e6 + 5;

ll l[MAX], r[MAX], n, q, L, R;

int main() {
    scanf("%lld", &n);
    for (int i = 1; i <= n; ++i) { scanf("%lld%lld", &l[i], &r[i]); }
    scanf("%lld", &q);
    for (int i = 1; i <= q; ++i) {
        std::set <ll> s; ll mex = 0;
        scanf("%lld%lld", &L, &R);
        for (int j = L; j <= R; ++j) {
            ll x = mex;
            if (x >= l[j] and x <= r[j]) {
                x = r[j] + 1;
            }
            while (s.count(x)) { ++x; } s.insert(x);
            while (s.count(mex)) { ++mex; }
        }
        printf("%lld\n", mex);
    }
}

以后不要练这种模拟赛了。