首页 > 其他分享 >牛客练习赛127

牛客练习赛127

时间:2024-07-06 17:56:34浏览次数:8  
标签:std 练习赛 return constexpr int res 牛客 127 MInt

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);
    #define int long long
    int n, k;
    std::cin >> n >> k;
    std::vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> a[i];
    }
    int ans = 0;
    std::sort(a.rbegin(), a.rend());
    for (int i = 1; i < n; ++i) {
        if (a[0] - a[i] > k) {
            ans = std::max(ans, std::max(a[0], a[i]));
        } else {
            ans = std::max(ans, std::min(a[0], a[i]));
        }
    }
    std::cout << ans << '\n';
}

B、小红的约数

代码实现

#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 w, d;
    std::cin >> w >> d;
    std::vector<std::pair<Z, int>> fac(w);
    for (auto &[p, a] : fac) {
        std::cin >> p >> a;
        p = power(p, d);
    }
    Z ans = 0;
    for (int i = 0; i < (int)size(fac); ++i) {
        auto [p, a] = fac[i];
        Z mul = a;
        if (p != 1) {
            mul = p * (power(p, a) - 1) / (p - 1);
        }
        debug(mul);
        ans += ans * mul + mul;
    }
    std::cout << ans + 1 << '\n';
}

C、小红的图上划分

代码实现

#include <bits/stdc++.h>

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

struct DSU {
    std::vector<int> p, siz;
    DSU(int n) : p(n), siz(n, 1) { std::iota(p.begin(), p.end(), 0); }
    int leader(int x) {
        while (x != p[x]) x = p[x] = p[p[x]];
        return x;
    }
    bool same(int x, int y) { return leader(x) == leader(y); }
    bool merge(int x, int y) {
        x = leader(x), y = leader(y);
        if (x == y) return false;
        siz[x] += siz[y], p[y] = x;
        return true;
    }
    int size(int x) { return siz[leader(x)]; }
};
int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
    int n, m, q;
    std::cin >> n >> m >> q;
    std::vector<std::array<int, 3>> edges(m);
    for (auto &[w, u, v] : edges) {
        std::cin >> u >> v >> w;
        u--, v--;
    }
    std::sort(edges.rbegin(), edges.rend());
    DSU dsu(n);
    int block = n;
    std::vector<int> ans(n, -2e9);
    for (auto &[w, u, v] : edges) {
        if (dsu.merge(u, v)) {
            block--;
            ans[block] = w;
        }
    }
    while (q--) {
        int L, R;
        std::cin >> L >> R;
        if (ans[R] == -2e9) {
            std::cout << "NO ANSWER" << '\n';
        } else {
            std::cout << ans[R] << '\n';
        }
    }
}

D、小红的白点距离

分析

发现题目的本质就是去除k个点后问树的直径最小是多少,因此直接枚举每个节点为根节点root时,保留距离root最近的\(n-k\)个点,对这些点求一遍直径,取最小值即可。

代码实现

#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, k;
    std::cin >> n >> k;
    std::vector<std::vector<int>> g(n);
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        std::cin >> u >> v;
        u--, v--;
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }
    int ans = 1e9;
    for (int root = 0; root < n; ++root) {
        std::vector<int> dep(n, -1), q;
        std::vector<bool> vis(n);
        q.emplace_back(root), dep[root] = 0;
        int c = root;
        for (int i = 0; i < n - k; ++i) {
            auto u = q[i];
            vis[u] = true;
            if (dep[c] < dep[u]) {
                c = u;
            }
            for (auto v : g[u]) if (dep[v] == -1) {
                dep[v] = dep[u] + 1;
                q.emplace_back(v);
            }
        }
        auto dfs = [&](auto &&self, int u, int fa)->void {
            for (auto v : g[u]) if (v != fa && vis[v]) {  
                dep[v] = dep[u] + 1;
                if (dep[c] < dep[v]) {
                    c = v;
                }
                self(self, v, u);
            }
        };
        debug(root, c, dep);
        dep[c] = 0, dfs(dfs, c, -1);
        debug(root, c, dep);
        ans = std::min(ans, dep[c]);
    }
    std::cout << ans << '\n';
}

标签:std,练习赛,return,constexpr,int,res,牛客,127,MInt
From: https://www.cnblogs.com/sleeeeeping/p/18287496

相关文章

  • 解决Pycharm配置R语言环境报错RWrapper terminated, exitcode: 127
    问题解决Pycharm配置R语言环境报错RWrapperterminated,exitcode:127errorwhileloadingsharedlibraries:libR.so:site:stackoverflow.com解决方案1.打开GetEnvVars.R文件打开C:\Users\UserName\AppData\Roaming\JetBrains\PyCharm版本号\plugins\r-plugin\R\目录......
  • 牛客练习赛127
    还没补E,感觉很GF。个人感觉质量挺好,拿CFDiv.2来对标也是出的比较好的一场。唯一的缺陷可能是E/F应该换个位置?简要写个题解?A给定数组\(a\),以及常数\(k\),定义\(w(i,j)\)当\(|a_i-a_j|>k\)时候为\(\max(a_i,a_j)\),否则为\(\min(a_i,a_j)\)。显然排序之后贪心将\(w......
  • 牛客小白月赛97 A-D题解
    AAAAAAAAAAAAAAAAAAAAA-----------------------------题解-------------------------------------------统计数组中有没有出现三个相同的边即可点击查看代码#include<bits/stdc++.h>usingnamespacestd;intmain(){intn;cin>>n;map<int,int>m;int......
  • [IOT2050 question] Unable to listen on http://127.0.0.1:1880/ 端口被占用错误
    1.背景第一次连接node-red的时候,一直出现错误Unabletolistenonhttp://127.0.0.1:1880/。如下:2.原因分析估计是早前利用iot2050setup小工具把node-red设置为开机自动启动项了,导致1880端口一直被占用。3.验证首先查看端口是否真的被占用,利用sudonetstat-ltup命......
  • (对结果分类讨论)牛客周赛 Round 1 C.游游的交换字符
    题意:思路:观察发现相邻元素不同的结果只有两种,要么是101010101...要么是010101010,因此我们可以对结果分类讨论。直接模拟算出两种情况最少需要操作多少次,再取min即可。需要注意的是,如果是奇数串,那么结果只有一种,数量多的一定要放两侧。code:#include<bits/stdc++.h>#includ......
  • 牛客周赛 Round 44
    A题每三张删除一张,n/3就是答案点击查看代码#include<bits/stdc++.h>#defineall(x)(x).begin(),(x).end()#definefifirst#definesesecondusingi64=longlong;usingpii=std::pair<int,int>;template<typenameT>std::vector<T>read(T&n......
  • 牛客周赛 Round 49 (D~F)
    嘤嘤不想求异或喵think:首先l和r的范围有1e18,我们能要到要么是二分(但这题显然和二分无关),所以我们尝试打表找规律.打表发现x是4的倍数,1~x的异或和应该是x,同理其他也是有规律的.#include<bits/stdc++.h>#definefifirst#definesesecond#defineintlonglongusin......
  • 牛客周赛 Round 49
    A题按题目输出即可#include<bits/stdc++.h>#defineall(x)(x).begin(),(x).end()#definefifirst#definesesecond#definelowbit(x)(x)&(-x)usingi64=longlong;usingpii=std::pair<int,int>;voidsolve(){i64a,b;std::cin>......
  • 牛客周赛49
    比赛链接:牛客周赛49赛时感受A思路    代码#include<bits/stdc++.h>usingnamespacestd;#definelllonglongconstintN=1e5+10;intmain(){lla,b;cin>>a>>b;cout<<a-b*11<<endl;return0;}B思路......
  • localhost 和 127.0.0.1 有什么区别?
    当前端开发人员在本地调试时,他们经常与 localhost 互动,只需运行npmrun命令就可以在浏览器中打开他们的网页,地址栏显示类似于 http://localhost:xxx/index.html的内容。许多人在使用它时可能没有思考两者之间的区别。考虑到我过去与开发人员合作时他们也缺乏对这两者区别......