牛客周赛 Round 47

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';
}
posted @ 2024-06-18 11:54  sleeeeeping  阅读(9)  评论(0编辑  收藏  举报