CodeForces 补题日常

CodeForces 补题日常

CodeForces1561C

原题链接

思路:

考虑二分,由于我们需要知道能不能成功出入每个洞,所以只需要记录最大值以及每个洞的怪物的个数
经过推理可知。在第i个怪物前面可以有i − 1个怪物杀死获得提升的机会,并且要严格大于怪物的头盔。
所以有 max = ai - i + 2 ;
然后二分答案判断能不能通过每个洞即可

#include <bits/stdc++.h>

using i64 = long long;
using PII = std::pair<int,int>;
#define int i64
#define yes std::cout << "YES\n";
#define no std::cout << "NO\n";

void solve() {
    int n;
    std::cin >> n;
    std::vector<std::pair<int,int>> ms(n);
    for (int i = 0; i < n; i ++) {
        int x;
        std::cin >> x;
        int max = 0;
        for (int j = 1; j <= x; j ++) {
            int t;
            std::cin >> t;
            max = std::max(max,t - j + 2);
    }
        ms[i] = {max,x};
    }
    std::sort(ms.begin(), ms.end());

    int l = ms[0].first,r = ms[n - 1].first;//也可以从1~1e9二分

    auto check = [&](int x) {
        for (int i = 0; i < n; i ++) {
            if (x >= ms[i].first) {
                x += ms[i].second; 
            } else {
                return false;
            }
        }
        return true;
    };

    while (l < r) {
        int mid = l + r >> 1;
        if (check(mid)) r = mid;
        else l = mid + 1;
    }
    std::cout << r << "\n";
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr); 
    
    int T;
    
    std::cin >> T;

    while (T -- ) {
        solve();
    }
    return 0;
}


CodeForces1561D1

原题链接

CodeForces1561D2

原题链接

思路:

转题解

#include <bits/stdc++.h>

using i64 = long long;
using PII = std::pair<int,int>;
// #define int i64
#define yes std::cout << "YES\n";
#define no std::cout << "NO\n";

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

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr); 
    
    int n;
    std::cin >> n >> P;

    std::vector<Z> dp(n + 10),sum(n + 10);
    dp[n] = sum[n] = 1;
    for (int i = n - 1; i >= 1; i --) {
        dp[i] = sum[i + 1];
        for (int j = 2; j * i <= n; j ++) {
            int l = i * j,r = std::min(n, j * (i + 1) - 1);
            dp[i] = dp[i] + sum[l] - sum[r + 1];
        }
        sum[i] = sum[i + 1] + dp[i];
    }
    std::cout << dp[1] << "\n";
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值