AtCoder Beginner Contest 420 题解 (A~E & G)
作者:桃李未来 沃若
微信号:
woruo27小红书:信竞教练沃若(
OI_woruo)也可邮件交流:[email protected]
2025.8.24,如有 Bug 请反馈
AtCoder Beginner Contest 420 题解
A - What month is it?
题目大意
给你介于
求
题目标签
- 模拟
- 数学
题解
可以通过取模运算直接得到结果,也可以对于
时间复杂度:
额外空间复杂度:
参考代码
#include <bits/stdc++.h>
using namespace std;
int x, y;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> x >> y;
cout << (x + y - 1) % 12 + 1 << '\n';
return 0;
}
B - Most Minority
题目大意
有 0 或 1,其中
对于每轮投票,如果所有人都投出 0 或 1,则所有人得
求出最终得分最高的所有人的编号。
题目标签
- 模拟
题解
按题意模拟即可。
时间复杂度:
额外空间复杂度:
参考代码
#include <bits/stdc++.h>
using namespace std;
int n, m;
string s[100];
int cnt[100];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> s[i];
for (int i = 0; i < m; i++) {
int x = 0, y = 0;
for (int j = 1; j <= n; j++) {
if (s[j][i] == '0')
x++;
else
y++;
}
if (x == 0 || y == 0) {
for (int j = 1; j <= n; j++)
cnt[j]++;
}
else if (x < y) {
for (int j = 1; j <= n; j++)
if (s[j][i] == '0')
cnt[j]++;
}
else {
for (int j = 1; j <= n; j++)
if (s[j][i] == '1')
cnt[j]++;
}
}
const int mx = *max_element(cnt + 1, cnt + n + 1);
vector<int> v;
for (int i = 1; i <= n; i++)
if (cnt[i] == mx)
v.push_back(i);
for (size_t i = 0; i < v.size(); i++)
cout << v[i] << "\n "[i + 1 < v.size()];
return 0;
}
C - Sum of Min Query
题目大意
有两个长度为
依次处理
每次询问后,输出
题目标签
- 贡献思想
题解
将每个位置的贡献
在每次询问时,先将该位置原有的贡献减去,再进行修改,最后加上新计算的贡献。
时间复杂度:
额外空间复杂度:
参考代码
#include <bits/stdc++.h>
using namespace std;
int n, q;
int a[200005];
int b[200005];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> q;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
cin >> b[i];
long long res = 0;
for (int i = 1; i <= n; i++)
res += min(a[i], b[i]);
for (int i = 1; i <= q; i++) {
char c;
int x, v;
cin >> c >> x >> v;
res -= min(a[x], b[x]);
if (c == 'A')
a[x] = v;
else
b[x] = v;
cout << (res += min(a[x], b[x])) << '\n';
}
return 0;
}
D - Toggle Maze
题目大意
有大小为
其中障碍物及关闭的门是无法通行的,但每次经过开关时,所有打开的门会关上,而关闭的门会打开。
每次可以向上下左右四个方向移动,给定原始地图及起点、终点,需要求出到达终点的最少步数,或报告无解。
题目标签
- BFS
- 建图
题解
本题的关键点在于处理门和开关。
在原地图基础上进行拆点建图,新增一维状态表示目前经过开关次数的奇偶性。
这样,在搜索的过程中就可以实时知道并维护门的开关状态。在这张新图上进行BFS即可求出最少步数。
时间复杂度:
额外空间复杂度:
参考代码
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};
int n, m;
string s[500];
int res[500][500][2];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> s[i];
memset(res, 0x3f, sizeof(res));
queue<tuple<int, int, int>> q;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (s[i][j] == 'S')
res[i][j][0] = 0, q.emplace(i, j, 0);
while (!q.empty()) {
const auto [x, y, flag] = q.front();
q.pop();
if (s[x][y] == 'G') {
cout << res[x][y][flag] << '\n';
return 0;
}
for (int i = 0; i < 4; i++) {
const int X = x + dx[i], Y = y + dy[i];
if (0 <= X && X < n &&
0 <= Y && Y < m &&
s[X][Y] != '#' &&
(s[X][Y] != 'o' || flag == 0) &&
(s[X][Y] != 'x' || flag == 1) &&
res[X][Y][flag ^ (s[X][Y] == '?')] == INF)
res[X][Y][flag ^ (s[X][Y] == '?')] = res[x][y][flag] + 1,
q.emplace(X, Y, flag ^ (s[X][Y] == '?'));
}
}
cout << -1 << '\n';
return 0;
}
E - Reachability Query
题目大意
有一张
依次处理
- 给定两个点,在之间加一条边。
- 给定一个点,如果是白色就变为黑色,否则变为白色。
- 给定一个点,判断其是否可以通过若干条边到达黑色的点。
题目标签
- 并查集
题解
对于加边类的题目,容易想到使用并查集维护。
对于第一种询问,直接在并查集中合并两个点所在的连通块即可。已连通则可以跳过。
对于第二、三种询问,我们需要维护每个连通块中的黑色点个数。当前连通块中黑色点个数不为 Yes,否则输出 No。
在加边合并时,需要将子节点的黑色点个数加给父亲。修改点的颜色时,需要将对应连通块记录的黑色点个数加一或减一。
时间复杂度:
额外空间复杂度:
参考代码
#include <bits/stdc++.h>
using namespace std;
int n, q;
bool c[200005];
int dsu[200005];
int cnt[200005];
int root(int x) {
while (dsu[x] != x)
x = dsu[x] = dsu[dsu[x]];
return x;
}
void merge(int x, int y) {
if (x != y)
dsu[x] = y, cnt[y] += cnt[x];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> q;
iota(dsu + 1, dsu + n + 1, 1);
while (q--) {
int type;
cin >> type;
if (type == 1) {
int u, v;
cin >> u >> v;
merge(root(u), root(v));
}
else if (type == 2) {
int v;
cin >> v;
cnt[root(v)] += (c[v] = !c[v]) ? 1 : -1;
}
else {
int v;
cin >> v;
cout << (cnt[root(v)] != 0 ? "Yes" : "No") << '\n';
}
}
return 0;
}
G - sqrt(n²+n+X)
题目大意
给定整数
题目标签
- 数学
题解
设
可以证明
时间复杂度:
额外空间复杂度:
参考代码
#include <bits/stdc++.h>
using namespace std;
const int K = 15000000;
long long x;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> x;
vector<long long> v;
for (int i = -K; i <= K; i++)
if ((x - 1LL * i * i) % (i * 2 - 1) == 0)
v.push_back((x - 1LL * i * i) / (i * 2 - 1));
sort(begin(v), end(v));
v.erase(unique(begin(v), end(v)), end(v));
cout << v.size() << '\n';
for (size_t i = 0; i < v.size(); i++)
cout << v[i] << "\n "[i + 1 < v.size()];
return 0;
}