牛客周賽 Round 47

sleeeeeping發表於2024-06-18

A、小紅的葫蘆

水一篇

程式碼實現

#include <bits/stdc++.h>

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
    int n = 5;
    std::vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> a[i];
    }
    std::sort(a.begin(), a.end());
    debug(a);
    bool ok = false;
    if (a[0] == a[1] && a[1] == a[2] && a[2] != a[3] && a[3] == a[4]) {
        ok = true;
    }
    std::reverse(a.begin(), a.end());
    if (a[0] == a[1] && a[1] == a[2] && a[2] != a[3] && a[3] == a[4]) {
        ok = true;
    }
    std::cout << (ok ? "YES" : "NO") << "\n";
}

B、茉茉的密碼

程式碼實現

#include <bits/stdc++.h>

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
    int n;
    std::cin >> n;
    std::vector<std::string> a(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> a[i];
    }
    for (char c = 'a'; c <= 'z'; ++c) {
        bool ok = true;
        for (int i = 0; i < n; ++i) {
            ok &= a[i].find(c) != std::string::npos;
        }
        if (ok) {
            std::cout << c << '\n';
            return 0;
        }
    }
}

C、苗苗的氣球

程式碼實現

#include <bits/stdc++.h>

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
    int n;
    std::cin >> n;
    std::vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> a[i];
    }
    auto b = a;
    std::sort(a.begin(), a.end());
    int sum = std::accumulate(a.begin(), a.end(), 0LL);
    int ans = 0;
    if (n == 2) {
        ans += (b[0] > b[1]) + (b[0] < b[1]); 
    } else {
        if (sum > 2LL * a[n - 1]) {
            for (int i = 0; i < n; ++i) {
                ans += b[i] > (sum % 2 == 0);
            }
        } else {
            for (int i = 0; i < n; ++i) {
                ans += b[i] == a[n - 1];
            }
        }
    }
    std::cout << ans << '\n';
}

D、萌萌的好數

程式碼實現

#include <bits/stdc++.h>

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
    #define int long long
    int tt;
    for (std::cin >> tt; tt--;) {   
        int n;
        std::cin >> n;
        auto check = [&](int x) {
            x -= 3;
            int ans = (x + 3) / 3 + (x / 10 - x / 10 / 3);
            return x + 3 - ans >= n;
        };
        int l = 1, r = 1e18;
        while (l < r) {
            int mid = l + r >> 1;
            if (check(mid)) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        std::cout << r << '\n';
    }
}

E、 茜茜的計算器

分析

水平和豎直對稱分開考慮。
對於水平對稱可以發現只能放\(0, 1, 3, 8\)四個數,考慮全部組合數量為\(4 ^ n\)
對於豎直對稱可以發現只能放\(0, 2, 5, 8\)四個數,因為豎直對稱,因此只要考慮前\(\frac{n}{2}\)個位置如何擺放,方案數為\(4^{\frac{n}{2}}\)。對於n為奇數,可以發現中間只能擺放\(0, 8\),因此方案數需要再乘上2。
最後減去兩種方案的重合數\(2^{\frac{n + 1}{2}}\)即可。

程式碼實現

#include <bits/stdc++.h>

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

template <class T>
constexpr T power(T a, long long b) {
    T res = 1;
    for (; b; b /= 2, a *= a)
        if (b % 2) res *= a;
    return res;
}
template <int P>
struct MInt {
    int x;
    constexpr MInt() : x{} {}
    constexpr MInt(long long x) : x{norm(x % P)} {}
    constexpr int norm(int x) const {
        if (x < 0) x += P;
        if (x >= P) x -= P;
        return x;
    }
    constexpr int val() const { return x; }
    explicit constexpr operator int() const { return x; }
    constexpr MInt operator-() const {
        MInt res;
        res.x = norm(P - x);
        return res;
    }
    constexpr MInt inv() const {
        assert(x != 0);
        return power(*this, P - 2);
    }
    constexpr MInt &operator*=(MInt rhs) {
        x = 1ll * x * rhs.x % P;
        return *this;
    }
    constexpr MInt &operator+=(MInt rhs) {
        x = norm(x + rhs.x);
        return *this;
    }
    constexpr MInt &operator-=(MInt rhs) {
        x = norm(x - rhs.x);
        return *this;
    }
    constexpr MInt &operator/=(MInt rhs) { return *this *= rhs.inv(); }
    friend constexpr MInt operator*(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res *= rhs;
        return res;
    }
    friend constexpr MInt operator+(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res += rhs;
        return res;
    }
    friend constexpr MInt operator-(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res -= rhs;
        return res;
    }
    friend constexpr MInt operator/(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res /= rhs;
        return res;
    }
    friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
        long long v;
        is >> v;
        a = MInt(v);
        return is;
    }
    friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) { return os << a.val(); }
    friend constexpr bool operator==(MInt lhs, MInt rhs) { return lhs.val() == rhs.val(); }
    friend constexpr bool operator!=(MInt lhs, MInt rhs) { return lhs.val() != rhs.val(); }
};

template <int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();

// constexpr int P = 998244353;
constexpr int P = 1e9 + 7;
using Z = MInt<P>;
int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
    int n ;
    std::cin >> n;
    Z lr = power(Z(4), n / 2), ud = power(Z(4), n);
    Z cocide = power(Z(2), (n + 1) / 2);
    if (n & 1) {
        lr *= 2;
    }
    std::cout << lr + ud - cocide << '\n';
}

F、花花的地圖

程式碼實現

#include <bits/stdc++.h>

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
    int n, m;
    std::cin >> n >> m;
    std::vector<std::string> g(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> g[i];
    }
    std::vector<int> C(m);
    for (int i = 0; i < m; ++i) {
        std::cin >> C[i];
    }
    std::vector dist(n, std::vector<int>(m, 1e9));
    std::vector<std::vector<bool>> vis(n, std::vector<bool>(m));
    std::priority_queue<std::array<int, 3>, std::vector<std::array<int, 3>>, std::greater<>> q;
    q.push({0, 0, 0});
    dist[0][0] = 0;
    while (size(q)) {
        auto [dis, x, y] = q.top();
        q.pop();
        if (x == n - 1 && y == m - 1) {
            break;
        }
        vis[x][y] = true;
        for (auto [fx, fy] : {std::pair{x + 1, y}, {x - 1, y}, {x, y + 1}, {x, y - 1}}) {
            if (fx >= 0 && fx < n && fy >= 0 && fy < m && !vis[fx][fy]) {
                if (g[fx][fy] == '.') {
                    if (dist[fx][fy] <= dis) continue;
                    dist[fx][fy] = dis;
                    q.push({dis, fx, fy});
                } else {
                    for (int fx = 0; fx < n; ++fx) {
                        if (dist[fx][fy] <= dis + C[fy]) continue;
                        dist[fx][fy] = dis + C[fy];
                        q.push({dist[fx][fy], fx, fy});
                    }
                }
            }
        }
    }
    std::cout << dist[n - 1][m - 1] << '\n';
}