AtCoder Beginner Contest 419 题解 (A~F)
作者:桃李未来 沃若
微信号:
woruo27小红书:信竞教练沃若(
OI_woruo)也可邮件交流:[email protected]
2025.8.16,如有 Bug 请反馈
AtCoder Beginner Contest 419 题解
A - AtCoder Language
题目大意
给你一个字符串
如果 red,blue 或 green,分别输出 SSS,FFF 或 MMM。
如果都不是,则输出 Unknown。
题目标签
- 实现
- 选择结构
- 字符串
题解
按题意使用 if 模拟即可。
时间复杂度:
额外空间复杂度:
参考代码
#include <bits/stdc++.h>
using namespace std;
string s;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> s;
if (s == "red")
cout << "SSS" << '\n';
else if (s == "blue")
cout << "FFF" << '\n';
else if (s == "green")
cout << "MMM" << '\n';
else
cout << "Unknown" << '\n';
return 0;
}
B - Get Min
题目大意
依次处理
- 将给定整数
x 加入集合。 - 从集合中移除最小的数字并输出。
题目标签
- 实现
- STL
- 堆(优先队列)
题解
使用优先队列(std::priority_queue)模拟即可。
时间复杂度:
额外空间复杂度:
参考代码
#include <bits/stdc++.h>
using namespace std;
int n;
priority_queue<int, vector<int>, greater<int>> q;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
for (cin >> n; n--; ) {
int type, x;
cin >> type;
if (type == 1)
cin >> x, q.push(x);
else
cout << q.top() << '\n', q.pop();
}
return 0;
}
C - King's Summit
题目大意
在一张八连通的二维地图上,有
每秒每个人都可以向周围移动一格,求所有人在同一坐标相遇的最短时间。
题目标签
- 数学
- 结论
题解
首先可以发现横纵坐标之间独立,只需考虑一维的情况。
于是考虑数轴上的
对横纵坐标分别求解后,答案取较大的一个即可。
时间复杂度:
额外空间复杂度:
参考代码
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
int mnr = INT_MAX, mxr, mnc = INT_MAX, mxc;
for (int i = 1, r, c; i <= n; i++) {
cin >> r >> c;
mnr = min(mnr, r), mxr = max(mxr, r);
mnc = min(mnc, c), mxc = max(mxc, c);
}
cout << max(mxr - mnr + 1, mxc - mnc + 1) / 2 << '\n';
return 0;
}
D - Substr Swap
题目大意
给定长度为
随后有
输出最终的字符串
题目标签
- 前缀和
- 差分
题解
不失一般性地,我们考虑其中第
可以通过维护差分数组再计算前缀和得到每个坐标被覆盖的次数,从而得到答案。
事实上我们只需要判断奇偶性,也可以直接维护前缀异或和,本质上与维护前缀和相同。
时间复杂度:
额外空间复杂度:
参考代码
#include <bits/stdc++.h>
using namespace std;
int n, m;
string s;
string t;
int pre[500005];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
cin >> s;
cin >> t;
for (int i = 1, l, r; i <= m; i++)
cin >> l >> r, pre[l] ^= 1, pre[r + 1] ^= 1;
for (int i = 1; i <= n; i++)
cout << ((pre[i] ^= pre[i - 1]) == 0 ? s[i - 1] : t[i - 1]);
cout << '\n';
return 0;
}
E - Subarray Sum Divisibility
题目大意
给定一个长度为
求最小操作次数,使得每个长度为
题目标签
- 动态规划
题解
如果有
我们发现,去掉其中求和的公共部分,可以得到
这一结论说明,我们可以将所有的位置,按下标对
如果我们再保证前
于是我们可以先预处理出将所有下标对
接着我们设
使用刷表法计算即可。
时间复杂度:
额外空间复杂度:
参考代码
#include <bits/stdc++.h>
using namespace std;
int n, m, l;
int a[505];
int val[505][505];
int dp[505][505];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> l;
for (int i = 1; i <= n; i++)
cin >> a[i], a[i] %= m;
for (int i = 1; i <= l; i++)
for (int j = 0; j < m; j++)
for (int k = i; k <= n; k += l)
val[i][j] += (j + m - a[k]) % m;
memset(dp, 0x3f, sizeof(dp));
dp[0][0] = 0;
for (int i = 0; i < l; i++)
for (int j = 0; j < m; j++)
for (int k = 0; k < m; k++)
dp[i + 1][(j + k) % m] = min(dp[i + 1][(j + k) % m], dp[i][j] + val[i + 1][k]);
cout << dp[l][0] << '\n';
return 0;
}
F - All Included
题目大意
给定
求长度为
题目标签
- 状压DP
- AC自动机
- trie 图
题解
考虑状压DP:当前长度为
我们发现,为了能够在状态直接进行转移,我们还需要维护当前的后缀,才能在追加字符的同时判断输入的字符串
长度为
我们先对所有输入的模式串
根据AC自动机的结论,每当经过一个模板串的 endpos 在 fail 树中的子树,这个模板串就出现一次。所以我们还要做一次拓扑排序,以得到 fail 树上每个节点到根路径上,经过的所有模板串状态的并集。
具体实现请阅读参考代码。
时间复杂度:
额外空间复杂度:
参考代码
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998'244'353;
struct trie {
int c[26];
int fail;
int d;
int mask;
};
int n, m;
trie t[85];
int tot;
int dp[105][85][1 << 8];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
int cur = 0;
for (const auto& j : s) {
int& nxt = t[cur].c[j - 'a'];
if (nxt == 0)
nxt = ++tot;
cur = nxt;
}
t[cur].mask = 1 << i;
}
queue<int> q;
for (const auto& i : t[0].c)
if (i != 0)
q.push(i);
while (!q.empty()) {
auto& cur = t[q.front()];
q.pop();
for (int i = 0; i < 26; i++) {
if (cur.c[i] == 0)
cur.c[i] = t[cur.fail].c[i];
else {
t[cur.c[i]].fail = t[cur.fail].c[i];
t[t[cur.fail].c[i]].d++;
q.push(cur.c[i]);
}
}
}
for (int i = 0; i <= tot; i++)
if (t[i].d == 0)
q.push(i);
vector<int> v;
while (!q.empty()) {
int cur = q.front();
q.pop();
v.push_back(cur);
const int f = t[cur].fail;
if (--t[f].d == 0)
q.push(f);
}
reverse(begin(v), end(v));
for (const auto& i : v)
t[i].mask |= t[t[i].fail].mask;
dp[0][0][0] = 1;
const int K = 1 << n;
for (int i = 0; i < m; i++)
for (int j = 0; j <= tot; j++)
for (int k = 0; k < K; k++)
for (const auto& c : t[j].c)
if ((dp[i + 1][c][k | t[c].mask] += dp[i][j][k]) >= MOD)
dp[i + 1][c][k | t[c].mask] -= MOD;
long long res = 0;
for (int i = 0; i <= tot; i++)
res += dp[m][i][K - 1];
cout << res % MOD << '\n';
return 0;
}