跳到正文

题解

AtCoder Beginner Contest 420 题解 (A~E & G)

沃若约 12 分钟
原文
文章信息与版本

文章信息

发布
2025年8月25日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 12 分钟

版本

  1. v1当前

作者:桃李未来 沃若

微信号:woruo27

小红书:信竞教练沃若(OI_woruo)

也可邮件交流:woruo@woruo.online

2025.8.24,如有 Bug 请反馈

AtCoder Beginner Contest 420 题解

A - What month is it?

题目大意

给你介于 11 和 1212 之间的整数 XX 和 YY。

求 XX 月之后的 YY 月是几月(例如,11 月是一月)。

题目标签

  • 模拟
  • 数学

题解

可以通过取模运算直接得到结果,也可以对于 XX 迭代 YY 次,每次求出下一个月的编号。

时间复杂度:O(1)O(1)。

额外空间复杂度:O(1)O(1)。

参考代码

C++
#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

题目大意

有 NN 个人进行 MM 轮投票,每轮每个人投出 0 或 1,其中 NN 是奇数。

对于每轮投票,如果所有人都投出 0 或 1,则所有人得 11 分。否则少数方每人得 11 分。

求出最终得分最高的所有人的编号。

题目标签

  • 模拟

题解

按题意模拟即可。

时间复杂度:O(N⋅M)O(N \cdot M)。

额外空间复杂度:O(N)O(N)。

参考代码

C++
#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

题目大意

有两个长度为 NN 的整数数组 AA 和 BB。

依次处理 QQ 次询问,每次会指定其中一个数组中的一个元素,并修改为指定的值。

每次询问后,输出 ∑i=1Nmin⁡(Ai,Bi)\sum_{i=1}^N \min(A_i, B_i)。

题目标签

  • 贡献思想

题解

将每个位置的贡献 min⁡(Ai,Bi)\min(A_i, B_i) 视作维护对象。

在每次询问时,先将该位置原有的贡献减去,再进行修改,最后加上新计算的贡献。

时间复杂度:O(N+Q)O(N + Q)。

额外空间复杂度:O(1)O(1)。

参考代码

C++
#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

题目大意

有大小为 N×MN \times M 的矩形地图,其中包含障碍物、打开的门、关闭的门、开关。

其中障碍物及关闭的门是无法通行的,但每次经过开关时,所有打开的门会关上,而关闭的门会打开。

每次可以向上下左右四个方向移动,给定原始地图及起点、终点,需要求出到达终点的最少步数,或报告无解。

题目标签

  • BFS
  • 建图

题解

本题的关键点在于处理门和开关。

在原地图基础上进行拆点建图,新增一维状态表示目前经过开关次数的奇偶性。

这样,在搜索的过程中就可以实时知道并维护门的开关状态。在这张新图上进行BFS即可求出最少步数。

时间复杂度:O(N⋅M)O(N \cdot M)。

额外空间复杂度:O(N⋅M)O(N \cdot M)。

参考代码

C++
#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

题目大意

有一张 NN 个点的无向图,最初每个点都是白色,没有任何边。

依次处理 QQ 次询问,分为三种类型。

  1. 给定两个点,在之间加一条边。
  2. 给定一个点,如果是白色就变为黑色,否则变为白色。
  3. 给定一个点,判断其是否可以通过若干条边到达黑色的点。

题目标签

  • 并查集

题解

对于加边类的题目,容易想到使用并查集维护。

对于第一种询问,直接在并查集中合并两个点所在的连通块即可。已连通则可以跳过。

对于第二、三种询问,我们需要维护每个连通块中的黑色点个数。当前连通块中黑色点个数不为 00 则输出 Yes,否则输出 No。

在加边合并时,需要将子节点的黑色点个数加给父亲。修改点的颜色时,需要将对应连通块记录的黑色点个数加一或减一。

时间复杂度:O(N+Qlog⁡N)O(N + Q \log N)。

额外空间复杂度:O(N)O(N)。

参考代码

C++
#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)

题目大意

给定整数 XX,求所有能让 n2+n+X\sqrt{n^2 + n + X} 为整数的 nn。

题目标签

  • 数学

题解

设 n+k=n2+n+Xn + k = \sqrt{n^2 + n + X},解得 n=X−k22k−1n = \frac{X - k^2}{2k - 1}。

可以证明 kk 的范围为 ±O(X)\pm O(\sqrt{X}),直接枚举 kk 得到可能的 nn,去重排序后输出。

时间复杂度:O(∣X∣+∣V∣log⁡∣V∣)O(\sqrt{|X|} + |V| \log |V|),其中 ∣V∣|V| 为 解集 ∣V∣|V| 的大小。

额外空间复杂度:O(∣V∣)O(|V|),其中 ∣V∣|V| 为 解集 ∣V∣|V| 的大小。

参考代码

C++
#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;
}
原文