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题表示是懵逼的,作不来

相关文章
相关标签/搜索