• AtCoder Beginner Contest 167


    比赛链接:https://atcoder.jp/contests/abc167/tasks

    A - Registration

    题意

    字符串 t 比字符串 s 长 1,除此外其余部分是否与 s 相同。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    int main() {
        string s, t; cin >> s >> t;
        cout << (s == t.substr(0, t.size() - 1) ? "Yes" : "No");
    }

    B - Easy Linear Programming

    题意

    有 a 个 1,b 个 0,c 个 -1,问从其中选取 k 个数能组成的最大值。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    int main() {
        int a[3] = {}, val[3] = {1, 0, -1};
        for (int i = 0; i < 3; i++) cin >> a[i];
        int k; cin >> k;
        int ans = 0;
        for (int i = 0; i < 3; i++) {
            int mi = min(k, a[i]);
            ans += mi * val[i], k -= mi;
        }
        cout << ans;
    }

    C - Skill Up

    题意

    初始时 m 个算法的能力均为 0,n 次中每次可以花费 ci 元提升 m 个算法的能力(提升程度可能不等),问 m 个算法能力都提升到不低于 x 的最少花费。

    题解

    数据范围较小,逐个枚举选取情况即可。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    
    const int INF = 1e9;
    int n, m, x;
    int c[15], a[15][15];
    int algo[15];
    int ans = INF, cost;
    
    bool ok() {
        for (int i = 0; i < m; i++)
            if (algo[i] < x) return false;
        return true;
    }
    
    void dfs(int i) {
        if (i == n) {
            if (ok()) ans = min(ans, cost);
            return;
        }
        cost += c[i];
        for (int j = 0; j < m; j++) algo[j] += a[i][j];
        dfs(i + 1);
        cost -= c[i];
        for (int j = 0; j < m; j++) algo[j] -= a[i][j];
        dfs(i + 1);
    }
    
    int main() {
        cin >> n >> m >> x;
        for (int i = 0; i < n; i++) {
            cin >> c[i];
            for (int j = 0; j < m; j++) cin >> a[i][j];
        }
        dfs(0);
        cout << (ans == INF ? -1 : ans);
    }

    D - Teleporter

    题意

    1 ~ n 个城镇每个城镇有一个传送点可以传送到其他城镇,问从第一个城镇出发传送 k 次后所在的城镇。

    题解

    标记环路的起点,加上传送次数对环路长度的模值即可,需要注意有可能先在一些城镇间传送后才进入环路。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    
    vector<int> path, cycle;
    map<int, bool> mp;
    
    int main() {
        long long n, k; cin >> n >> k;
        int a[n + 1] = {}; 
        for (int i = 1; i <= n; i++) cin >> a[i];
        for (int i = 1; ; i = a[i]) {
            if (!mp[a[i]]) {
                path.push_back(a[i]);
                mp[a[i]] = true;
            } else {
                bool flag = false;
                for (int j : path) {
                    if (j == a[i]) flag = true;
                    if (flag) cycle.push_back(j);
                }
                break;
            }
        }
        if (k <= path.size()) cout << path[k - 1];
        else {
            k -= path.size();
            k %= cycle.size();
            cout << cycle[(k - 1 + cycle.size()) % cycle.size()];
        }
    }

    E - Colorful Blocks

    题意

    给 n 个方块染色,可以使用 m 个颜色,要求最多有 k 对相邻方块同色。

    题解

    $ans = sum_{i = 0}^{k} m * C_{n-1}^{i} * (m - 1) ^ {n - 1 - i}$

    即第 1 个方块可以染 m 种颜色,从余下 n - 1 个方块中选取 i 个方块,这 i 个方块组成同色的 i 对方块,它们的颜色与左相邻的方块相同,其余的 n - 1 - i 个方块因为不与左相邻方块同色,每个可以染 m - 1 个颜色。

    代码

    #include <bits/stdc++.h>
    using LL = long long;
    using namespace std;
    
    const LL M = 2e5 + 100;
    const LL mod = 998244353;
    LL fac[M];
    
    LL add(LL a, LL b) {
        return (a + b) % mod;
    }
    
    LL mul(LL a, LL b) {
        return a * b % mod;
    }
    
    LL fpow(LL a, LL b) {
        LL res = 1;
        while (b > 0) {
            if (b & 1) res = mul(res, a);
            a = mul(a, a);
            b >>= 1;
        }
        return res;
    }
    
    LL inv(LL a) {
        return fpow(a, mod - 2);
    }
    
    LL C(LL n, LL m) {
        return mul(fac[n], mul(inv(fac[m]), inv(fac[n - m])));
    }
    
    void init() {
        fac[0] = 1;
        for (int i = 1; i < M; i++) fac[i] = mul(fac[i - 1], i);
    }
    
    int main() {
        init();
        LL n, m, k; cin >> n >> m >> k;
        LL ans = 0;
        for (int i = 0; i <= k; i++)
            ans = add(ans, mul(m, mul(C(n - 1, i), fpow(m - 1, n - 1 - i))));
        cout << ans;
    }

    F - Bracket Sequencing

    题意

    能否将一些括号串编排为合法串。

    题解

    原题:gym - 101341 - A

    代码

    #include <bits/stdc++.h>
    using namespace std;
    
    const int M = 1e6 + 100;
    int l[M], r[M];
    int id[M], tp[M];
    
    int main() {
        int n; cin >> n;
        for (int i = 0; i < n; i++) {
            string s; cin >> s;
            int x = 0, y = 0;
            for (char c : s) {
                if (c == '(') ++x;
                else if (x) --x;
                else ++y;
            }
            l[i] = x, r[i] = y, id[i] = i;
            if (y == 0) tp[i] = 1;
            else if (x == 0) tp[i] = 4;
            else if (x >= y) tp[i] = 2;
            else tp[i] = 3;
        }
        sort(id, id + n, [&] (int a, int b) {
            if (tp[a] != tp[b]) return tp[a] < tp[b];
            if (tp[a] == 2) {
                if (r[a] != r[b]) return r[a] < r[b];
                else return l[a] > l[b];
            }
            if (tp[a] == 3) return l[a] > l[b];
            return false;
        });
        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum -= r[id[i]];
            if (sum < 0) break;
            sum += l[id[i]];
        }
        cout << (sum == 0 ? "Yes" : "No");
    }
  • 相关阅读:
    POJ2175 Evacuation Plan
    POJ3252 Round Numbers
    POJ2115 C Looooops
    POJ3422 Kaka's Matrix Travels
    POJ1659 Frogs' Neighborhood
    POJ2635 The Embarrassed Cryptographer
    POJ3436 ACM Computer Factory
    FZU1607 Greedy division
    EOJ440 Buying Feed
    POJ2135 Farm Tour
  • 原文地址:https://www.cnblogs.com/Kanoon/p/12865555.html
Copyright © 2020-2023  润新知