练习赛140 - 赛后ABCD

A

void kdog(int test) {
    int n, m; cin >> n >> m;
    string s; cin >> s;
    if (m > n) {
        cout << "-1\n";
        return;
    }
    cout << s.substr(0, m) << "\n";
} 

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    T = 1;
    cin >> T;
    init();
    for (int test = 1; test <= T; test++) {
        kdog(test);
    }
    return 0;
}

B

void kdog(int test) {
    int s, n; cin >> n >> s;
    int t1 = n / s, t2 = t1 + 1;
    int c1 = t1 + abs(n / t1 - s);
    int c2 = t2 + abs(n / t2 - s);
    cout << min({s - 1, c1, c2}) << "\n";
} 

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    T = 1;
    cin >> T;
    init();
    for (int test = 1; test <= T; test++) {
        kdog(test);
    }
    return 0;
}

C

void kdog(int test) {
    int n; cin >> n;
    vi a(n); cin >> a;
    vi cnt(110);
    for (int x: a) cnt[x]++;
    int p = 0, q = 0;
    for (int i = 1; i <= 100; i++) {
        if (cnt[i] > 1) {
            cout << "-1\n";
            return;
        }
        if (cnt[i]) {
            if (i > n) {
                cout << "-1\n";
                return;
            }
            if (i - p > 2) {
                cout << "-1\n";
                return;
            } else if (i - p == 2) {
                q++;
                if (q > 1) {
                    cout << "-1\n";
                    return;
                }
            }
            p = i;
        }
    }

    vi res;
    auto dfs = [&](auto &&self) -> void {
        map<int, int> idx;
        int mx = 0;
        for (int i = 0; i < n; i++) idx[a[i]] = i, chmax(mx, a[i]);
        if (idx.size() == 1 && idx.count(0)) return;
        res.push_back(idx[mx] + 1);
        int ne = 0;
        for (int i = 1; i <= mx; i++) {
            if (!idx.count(i)) {
                ne = i;
                break;
            }
        }
        a[idx[mx]] = ne;
        self(self);
    };
    dfs(dfs);
    reverse(all(res));
    cout << res.size() << "\n";
    cout << res << "\n";
} 

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    T = 1;
    cin >> T;
    init();
    for (int test = 1; test <= T; test++) {
        kdog(test);
    }
    return 0;
}

D

void kdog(int test) {
    int n; cin >> n;
    vi a(n); cin >> a;
    vector<vector<pii>> e(n);
    for (int i = 0; i < n - 1; i++) {
        int u, v, w; cin >> u >> v >> w;
        u--, v--;
        e[u].push_back({v, w});
        e[v].push_back({u, w});
    }
    int q; cin >> q;
    while (q--) {
        int s, x; cin >> s >> x;
        s--, x--;
        vi pre(n), path(n);
        auto dfs1 = [&](auto &&self, int u, int fa) -> void {
            pre[u] = fa;
            for (auto [v, w]: e[u]) {
                if (v == fa) continue;
                self(self, v, u);
            }
        };
        dfs1(dfs1, s, -1);
        int ed = x;
        while (ed != s) {
            path[ed] = 1;
            ed = pre[ed];
        }
        path[s] = 1;
        vi f(n);
        auto dfs2 = [&](auto &&self, int u, int fa) -> void {
            f[u] = a[u];
            for (auto [v, w]: e[u]) {
                if (v == fa) continue;
                self(self, v, u);
                if (path[v]) {
                    f[u] += f[v] - w;
                } else {
                    f[u] += max(0ll, f[v] - 2 * w);
                }
            }
        };
        dfs2(dfs2, s, -1);
        cout << f[s] << "\n";
    }
} 

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    T = 1;
    cin >> T;
    init();
    for (int test = 1; test <= T; test++) {
        kdog(test);
    }
    return 0;
}
posted @ 2025-06-08 14:58  k(d)o(g)  阅读(18)  评论(0)    收藏  举报