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");
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM