AtCoder Beginner Contest 198 我的題解(AB水題,C思惟,D思惟+全排列,E題DFS搜索,F懵逼)

補題連接:Hereios

A - Div

題意:N 個不同的糖,請問有多少種分法給 A,B兩人c++

水題,寫幾組狀況就能知道輸出 \(N - 1\) 便可spa

B - Palindrome with leading zeros

題意:給定一個字符串,問是否能夠在字符串前加若干個 0 使字符串迴文code

先判斷一下字符串迴文否?自己就回文就無需處理,否則字符串後面有幾個 0 就加上多少,而後再判斷ci

C - Compass Walking

題意:在一個二維座標軸上,給定一個長度 R ,請問是否有最小步數(每步只能走 R,但座標能夠非整數)到達 \((X,Y)\)字符串

思路:get

假設 \(d\) 爲 起點\((0,0)\)\((X,Y)\) 的歐幾里得距離,則容易想到如下三種狀況string

  • 答案爲 \(1\) ,若是 \(d = R\)
  • 答案爲 \(2\) ,若是 \(d \ne R\) 而且 \(d < R\)
  • \(\lceil \frac{d}{R} \rceil\) ,其餘狀況

其實這裏 第一種狀況和第三種狀況可合併寫:ceil(d / R)it

void solve() {
    double R, X, Y;
    cin >> R >> X >> Y;
    double d = sqrt(X * X + Y * Y);
    if (d < R) cout << 2;
    else
        cout << ceil(d / R);
}

D - Send More Money

題意:給定 \(3\) 個字符串 \(N_1,N_2,N_3\) 試問是否有數字能代替某種字母使得 \(N_1 + N_2 = N_3\)io

思路:

首先,若是出現 \(10\) 種以上的字母,那麼確定是沒法解決的,直接輸出 UNSOLVABLE 便可

對於剩下的狀況來講,能夠嘗試把數字分配給字母,而後 check 一下 \(N_1 + N_2 = N_3\)

\(10 ! = 3628800\) 是可執行範圍內

注意別給首位分配 \(0\) 便可

void solve() {
    map<char, int> ch;
    string s, t, w;
    cin >> s >> t >> w;
    for (char c : s) ch.emplace(c, 0);
    for (char c : t) ch.emplace(c, 0);
    for (char c : w) ch.emplace(c, 0);
    if (ch.size() > 10) {
        cout << "UNSOLVABLE";
        return;
    }
    int p[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    do {
        string a, b, c;
        int i = 0;
        for (auto it = ch.begin(); it != ch.end(); ++it, i++)
            it->second = p[i];
        for (char x : s) a.push_back(ch[x] + '0');
        for (char x : t) b.push_back(ch[x] + '0');
        for (char x : w) c.push_back(ch[x] + '0');
        ll A = stoll(a), B = stoll(b), C = stoll(c);
        if (a[0] != '0' && b[0] != '0' && c[0] != '0' && A + B == C) {
            cout << a << "\n"
                 << b << "\n"
                 << c << "\n";
            return;
        }
    } while (next_permutation(p, p + 10));
    cout << "UNSOLVABLE";
}

E - Unique Color

題意:

思路:用 DFS 搜索一下便可

// Murabito-B 21/04/12
#include <bits/stdc++.h>
using namespace std;
using ll    = long long;
const int N = 100005;
int n, c[N], cnt[N], good[N];
vector<int> to[N];
void dfs(int u, int fa) {
    if (cnt[c[u]] == 0) good[u] = 1;
    cnt[c[u]]++;
    for (int i = 0, v; i < to[u].size(); i++)
        if ((v = to[u][i]) != fa) dfs(v, u);
    cnt[c[u]]--;
}
void solve() {
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> c[i];
    for (int i = 1, u, v; i < n; ++i) cin >> u >> v, to[u].push_back(v), to[v].push_back(u);
    dfs(1, 0);
    for (int i = 1; i <= n; ++i)
        if (good[i]) cout << i << "\n";
}
int main() {
    ios_base::sync_with_stdio(false), cin.tie(0);
    solve();
    return 0;
}

F題表示是懵逼的,作不來

相關文章
相關標籤/搜索