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;
}