ZZJC 2024 autumn Individual Contest - 13(题解)
预测难度
Easy:A、E
Medium:C、D
Hard:B、F
A - 我不是签到题
代码实现
#include <bits/stdc++.h>
#ifdef LOCAL
#include "algo/debug.hpp"
#else
#define debug(...) 42
#endif
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
std::array<int, 5> a, b;
for (int i = 0; i < 5; ++i) {
std::cin >> a[i];
}
for (int i = 0; i < 5; ++i) {
std::cin >> b[i];
}
if (std::accumulate(b.begin(), b.end(), 0) > std::accumulate(a.begin(), a.end(), 0)) {
std::cout << "Red" << '\n';
} else {
std::cout << "Blue" << '\n';
}
}
E - 我不是模拟题
代码实现
#include <bits/stdc++.h>
#ifdef LOCAL
#include "algo/debug.hpp"
#else
#define debug(...) 42
#endif
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(20);
int tt;
for (std::cin >> tt; tt--;) {
double c, n;
std::cin >> n >> c;
if (n >= 10000000) {
n = 2.0;
} else if (n <= 9800000) {
n = (n - 9500000.0) / 300000.0;
} else {
n = 1.0 + (n - 9800000.0) / 200000.0;
}
std::cout << std::max(0.0, n + c) << '\n';
}
}
C - 我不是简单题
代码实现
#include <bits/stdc++.h>
#ifdef LOCAL
#include "algo/debug.hpp"
#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 x, y, a, b;
std::cin >> x >> y >> a >> b;
int d = std::gcd(x, y);
x /= d, y /= d;
if (x % 2 && y % 2 && __builtin_popcountll(x + y) == 1) {
std::cout << __builtin_ctzll(x + y) + 1 << '\n';
} else {
std::cout << -1 << '\n';
}
}
}
D - 我不是大坑题
代码实现
#include <bits/stdc++.h>
#ifdef LOCAL
#include "algo/debug.hpp"
#else
#define debug(...) 42
#endif
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
int n;
std::cin >> n;
if (n == 1) {
std::cout << 1 << "\n";
std::cout << 1 << '\n';
} else if (n <= 3) {
std::cout << 2 << "\n";
std::cout << 2 << " " << 2 << '\n';
} else {
std::cout << (n - 2) * 2 << '\n';
for (int i = 2; i <= n - 1; ++i) {
std::cout << i << ' ';
}
for (int i = n - 1;i >= 2; --i) {
std::cout << i << " \n"[i == 2];
}
}
}
B - 我不是并查集
代码实现
#include <bits/stdc++.h>
#ifdef LOCAL
#include "algo/debug.hpp"
#else
#define debug(...) 42
#endif
using i64 = long long;
struct DSU {
std::vector<int> p, siz;
DSU(int n) { init(n); }
void init(int n) {
p.resize(n);
std::iota(p.begin(), p.end(), 0);
siz.assign(n, 1);
}
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, m1, m2, t;
std::cin >> n >> m1 >> m2 >> t;
if (t == 0) {
std::cout << -1 << '\n';
return 0;
}
std::vector g(n, std::vector<i64> (n, 1E18));
while (m1--) {
i64 a, b, c;
std::cin >> a >> b >> c;
a--, b--;
assert(a < n && b < n);
g[a][b] = g[b][a] = std::min(g[a][b], c);
}
std::vector<int> s(m2);
std::vector<bool> f(n);
for (auto &x : s) {
std::cin >> x, x--;
f[x] = true;
}
for (int k = 0; k < n; ++k) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
g[i][j] = std::min(g[i][j], g[i][k] + g[k][j]);
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (g[i][j] >= (i64)1e17) {
std::cout << -1 << '\n';
return 0;
}
}
}
std::vector<std::tuple<i64, int, int>> edges;
for (auto i : s) {
for (int j = 0; j < n; ++j) {
if (f[i] && f[j]) {
edges.push_back({g[i][j], i, j});
} else {
edges.push_back({g[i][j] * 2LL, i, j});
}
}
}
sort(edges.begin(), edges.end());
i64 ans = 0;
DSU dsu(n);
for (auto [c, a, b] : edges)
if (!dsu.same(a, b)) {
dsu.merge(a, b);
ans = c;
}
std::cout << (ans + t - 1) / t << '\n';
}
F - 我不是线段树
代码实现
#include <bits/stdc++.h>
#ifdef LOCAL
#include "algo/debug.hpp"
#else
#define debug(...) 42
#endif
using i64 = long long;
template<class Info, class Tag>
struct LazySegTree {
int n;
std::vector<Info> tr;
std::vector<Tag> tag;
LazySegTree(int n) : n(n), tr(4 << std::__lg(n)), tag(4 << std::__lg(n)) {}
LazySegTree(int n, std::vector<Info> a) : LazySegTree(n) {
std::function<void(int, int, int)> build = [&](int u, int l, int r) {
if (l == r) {
tr[u] = a[r];
return ;
}
int mid = l + r >> 1;
build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
pushup(u);
};
build(1, 1, n);
}
void pushup(int u) { tr[u] = tr[u << 1] + tr[u << 1 | 1]; };
void apply(int u, const Tag &v) { tr[u].apply(v), tag[u].apply(v); }
void pushdown(int u) {
apply(u << 1, tag[u]), apply(u << 1 | 1, tag[u]);
tag[u] = Tag();
}
void modify(int u, int l, int r, int x, const Info &v) {
if (l == r) {
tr[u].n = v.n;
return ;
}
int mid = l + r >> 1;
pushdown(u);
if (x <= mid) modify(u << 1, l, mid, x, v);
else modify(u << 1 | 1, mid + 1, r, x, v);
pushup(u);
}
void modify(int x, const Info &v) { modify(1, 1, n, x, v); }
Info query(int u, int l, int r, int ql, int qr) {
if (l > qr || r < ql) return Info();
if (ql <= l && r <= qr) return tr[u];
int mid = l + r >> 1;
pushdown(u);
return query(u << 1, l, mid, ql, qr) + query(u << 1 | 1, mid + 1, r, ql, qr);
}
Info query(int l, int r) { return query(1, 1, n, l, r); }
void rangeApply(int u, int l, int r, int ql, int qr, const Tag &v) {
if (l > qr || r < ql) return ;
if (l >= ql && r <= qr) {
apply(u, v);
return ;
}
int mid = l + r >> 1;
pushdown(u);
rangeApply(u << 1, l, mid, ql, qr, v), rangeApply(u << 1 | 1, mid + 1, r, ql, qr, v);
pushup(u);
}
void rangeApply(int l, int r, const Tag &v) { return rangeApply(1, 1, n, l, r, v); }
};
struct Tag {
i64 add = 0;
void apply(const Tag &t) { add += t.add; }
};
struct Info {
int n;
i64 sum = 0;
void apply(const Tag &t) { sum += t.add * n; }
};
Info operator+(const Info &l, const Info &r) {
return {
l.n + r.n,
l.sum + r.sum
};
}
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
int n, m;
std::cin >> n >> m;
i64 ans = 0;
std::vector<Info> a(n + 1);
for (int i = 1; i <= n; ++i) {
i64 x;
std::cin >> x;
ans += x;
a[i].sum = x;
}
for (int i = 1; i <= n; ++i) {
int x;
std::cin >> x;
a[i].n = x;
}
LazySegTree<Info, Tag> seg(n, a);
while (m--) {
int op;
std::cin >> op;
if (op == 1) {
int x;
std::cin >> x;
seg.modify(x, {0, 0});
} else if (op == 2) {
int x;
std::cin >> x;
seg.modify(x, {1, 1});
} else if (op == 3) {
int l, r, x;
std::cin >> l >> r >> x;
seg.rangeApply(l, r, {x});
} else {
int l, r;
std::cin >> l >> r;
std::cout << seg.query(l, r).sum << "\n";
}
}
}