Codeforces Round 925 (Div. 3) A-G 题解 | JorbanS

A - Recovering a Small String

cpp 复制代码
string solve() {
    cin >> n;
    s = "aaa";
    n -= 3;
    if (n) s[2] += min(n, 25), n -= min(n, 25);
    if (n) s[1] += min(n, 25), n -= min(n, 25);
    if (n) s[0] += min(n, 25), n -= min(n, 25);
    return s;
}

B - Make Equal

cpp 复制代码
string solve() {
    cin >> n;
    ll res = 0, sum = 0;
    for (int i = 1; i <= n; i ++) cin >> a[i], sum += a[i];
    sum /= n;
    for (int i = 1; i <= n; i ++) {
        res += a[i];
        if (res < sum * i) return no;
    }
    return yes;
}

C - Make Equal Again

cpp 复制代码
int solve() {
    cin >> n;
    for (int i = 0; i < n; i ++) cin >> a[i];
    if (n == 1) return 0;
    int l = 0, r = n - 1;
    while (a[l] == a[l + 1] && l < n - 1) l ++;
    while (a[r] == a[r - 1] && r) r --;
    if (l > r) return 0;
    if (a[0] == a[n - 1]) return r - l - 1;
    return min(n - l - 1, r);
}

D - Divisible Pairs

cpp 复制代码
ll solve() {
    cin >> n >> x >> y;
    for (int i = 0; i < n; i ++) {
        int t; cin >> t;
        a[i] = t % x, b[i] = t % y;
    }
    map<pii, int> mp;
    ll res = 0;
    for (int i = 0; i < n; i ++) {
        res += mp[{(x - a[i]) % x, b[i]}];
        mp[{a[i], b[i]}] ++;
    }
    return res;
}

E - Anna and the Valentine's Day Gift

cpp 复制代码
string solve() {
    cin >> n >> m;
    ll res = 0, c0 = 0;
    for (int i = 0; i < n; i ++) {
        int x; cin >> x;
        a[i] = 0;
        if (!x) {
            c0 ++;
            res ++;
            continue;
        }
        while (x % 10 == 0) x /= 10, a[i] ++;
        while (x) x /= 10, res ++;
    }
    if (c0 == n) return aa;
    sort(a, a + n);
    reverse(a, a + n);
    for (int i = 0; i < n; i ++)
        if (i & 1) res += a[i];
    return res > m ? bb : aa;
}

F - Chat Screenshots

题意:每个人将自己放到最前面,其他人顺序不变

不管第一个人,看后面 n − 1 n-1 n−1 个人的顺序有没有冲突,即逆序对组成了环,拓扑排序即可

cpp 复制代码
string solve() {
    cin >> n >> m;
    vector<vector<int>> e(n + 1);
    vector<int> d(n + 1);
    while (m --)
        for (int i = 0; i < n; i ++) {
            cin >> a[i];
            if (i > 1) e[a[i - 1]].emplace_back(a[i]), d[a[i]] ++;
        }
    queue<int> q;
    for (int i = 1; i <= n; i ++)
        if (!d[i]) q.push(i);
    int cnt = 0;
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        cnt ++;
        for (auto v : e[u])
            if (-- d[v] == 0) q.push(v);
    }
    return cnt == n ? yes : no;
}

G - One-Dimensional Puzzle

挡板法

cpp 复制代码
int qpow(int a, int b) {
    int res = 1;
    while (b) {
        if (b & 1) res = (ll)res * a % mod;
        a = (ll)a * a % mod;
        b >>= 1;
    }
    return res;
}

inline int inv(int x) { return qpow(x, mod - 2); }

inline int C(int a, int b) { return (ll)fac[a] * infac[b] % mod * infac[a - b] % mod; }

ll solve() {
    cin >> c1 >> c2 >> c3 >> c4;
    if (abs(c1 - c2) > 1) return 0;
    if (c1 == 1 && !c2 || !c1 && c2 == 1) return 1;
    if (!c1 && !c2) return !(c3 && c4);
    if (!c3 && !c4) return (c1 == c2) + 1;
    ll res = 0;
    if (c1 >= c2) {
        int l = c2 + 1, r = c1;
        res += (ll)C(l + c4 - 1, c4) * C(r + c3 - 1, c3);
    }
    if (c2 >= c1) {
        int l = c2, r = c1 + 1;
        res += (ll)C(l + c4 - 1, c4) * C(r + c3 - 1, c3);
    }
    return res % mod;
}

signed main() {
    FastIO
    *fac = 1;
    for (int i = 1; i <= M; i ++) fac[i] = (ll)fac[i - 1] * i % mod;
    infac[M] = inv(fac[M]);
    for (int i = M - 1; i >= 0; i --) infac[i] = (ll)infac[i + 1] * (i + 1) % mod;
    Cases
    cout << solve() << endl;
    return 0;
}
相关推荐
luthane35 分钟前
python 实现average mean平均数算法
开发语言·python·算法
静心问道39 分钟前
WGAN算法
深度学习·算法·机器学习
杰九1 小时前
【算法题】46. 全排列-力扣(LeetCode)
算法·leetcode·深度优先·剪枝
manba_1 小时前
leetcode-560. 和为 K 的子数组
数据结构·算法·leetcode
liuyang-neu1 小时前
力扣 11.盛最多水的容器
算法·leetcode·职场和发展
忍界英雄1 小时前
LeetCode:2398. 预算内的最多机器人数目 双指针+单调队列,时间复杂度O(n)
算法·leetcode·机器人
Kenneth風车1 小时前
【机器学习(五)】分类和回归任务-AdaBoost算法-Sentosa_DSML社区版
人工智能·算法·低代码·机器学习·数据分析
C7211BA2 小时前
使用knn算法对iris数据集进行分类
算法·分类·数据挖掘
Tisfy2 小时前
LeetCode 2398.预算内的最多机器人数目:滑动窗口+单调队列——思路清晰的一篇题解
算法·leetcode·机器人·题解·滑动窗口
程序猿练习生2 小时前
C++速通LeetCode简单第18题-杨辉三角(全网唯一递归法)
c++·算法·leetcode