当前位置: 首页 > news >正文

AtCoder Beginner Contest竞赛题解 | ABC 421

​欢迎大家订阅我的专栏:算法题解:C++与Python实现!
本专栏旨在帮助大家从基础到进阶 ,逐步提升编程能力,助力信息学竞赛备战!

专栏特色
1.经典算法练习:根据信息学竞赛大纲,精心挑选经典算法题目,提供清晰的代码实现与详细指导,帮助您夯实算法基础。
2.系统化学习路径:按照算法类别和难度分级,从基础到进阶,循序渐进,帮助您全面提升编程能力与算法思维。

适合人群:

  • 准备参加蓝桥杯、GESP、CSP-J、CSP-S等信息学竞赛的学生
  • 希望系统学习C++/Python编程的初学者
  • 想要提升算法与编程能力的编程爱好者

附上汇总帖:AtCoder Beginner Contest竞赛题解 | 汇总


A - Misdelivery

【题目来源】

洛谷:[AT_abc421_a ABC421A] Misdelivery - 洛谷

【题目描述】

Mansion AtCoder has \(N\) rooms numbered from room \(1\) to room \(N\).
AtCoder公馆有 \(N\) 个房间,编号从 \(1\) 号房间到 \(N\) 号房间。

Each room \(i\) is inhabited by one person named \(S_i\).
每个房间 \(i\) 住着一位名为 \(S_i\) 的人。

You are to deliver a package addressed to Mr./Ms. \(Y\) in room \(X\). Determine whether the destination is correct.
您需要将收件人为 \(Y\) 先生/女士的包裹送至 \(X\) 号房间。请判断目的地是否正确。

【输入】

The input is given from Standard Input in the following format:

N
S_1
S_2
.
.
.
S_N
X Y

【输出】

Print Yes if the name of the person living in room X is Y, and No otherwise.

【输入样例】

3
sato
suzuki
takahashi
3 takahashi

【输出样例】

Yes

【算法标签】

《洛谷 AT_abc421_a Misdelivery》 #模拟#

【代码详解】

#include <bits/stdc++.h>
using namespace std;const int N = 105;  // 定义最大字符串数量int n;               // 字符串数量
string a[N];         // 存储字符串的数组int main()
{// 输入字符串数量cin >> n;// 输入n个字符串for (int i = 1; i <= n; i++){cin >> a[i];}int x;        // 要查询的字符串索引string y;     // 要比较的目标字符串// 输入查询参数cin >> x >> y;// 比较指定位置的字符串if (a[x] == y){cout << "Yes" << endl;  // 相等输出Yes}else{cout << "No" << endl;   // 不等输出No}return 0;
}

【运行结果】

3
sato
suzuki
takahashi
3 takahashi
Yes

C - Alternated

【题目来源】

洛谷:[AT_abc421_c ABC421C] Alternated - 洛谷

【题目描述】

You are given a string \(S\) of length \(2N\). \(S\) contains exactly \(N\) occurrences of A and \(N\) occurrences of B.
给定一个长度为 \(2N\) 的字符串 \(S\),其中恰好包含 \(N\)A\(N\)B

Find the minimum number of operations (possibly zero) needed to make \(S\) have no adjacent identical characters, where an operation consists of swapping two adjacent characters in \(S\).
求使字符串 \(S\) 不存在相邻相同字符所需的最小操作次数(可以为零),其中每次操作包含交换 \(S\) 中两个相邻字符。

【输入】

The input is given from Standard Input in the following format:

N 
S

【输出】

Print the answer.

【输入样例】

3
AABBBA

【输出样例】

2

【算法标签】

《洛谷 AT_abc421_d Alternated》 #贪心#

【代码详解】

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 500005;int n, cur; 
string s, s2;
int cntA[N], dstA[N], minn = 1e18;signed main()
{// 输入n和字符串scin >> n >> s;int len = s.size();s = " " + s;  // 在字符串前加空格,使索引从1开始// 统计所有'A'字符的位置,存入cntA数组for (int i = 1; i <= len; i++){if (s[i] == 'A'){cntA[++cur] = i;}}// 第一种目标位置方案:1,3,5,...,2n-1for (int i = 1; i <= n; i++){dstA[i] = 2 * i - 1;}// 计算第一种方案的总移动距离int sum = 0;for (int i = 1; i <= n; i++){sum += abs(cntA[i] - dstA[i]);}minn = min(minn, sum);// 第二种目标位置方案:2,4,6,...,2nfor (int i = 1; i <= n; i++){dstA[i] = 2 * i;}// 计算第二种方案的总移动距离sum = 0;for (int i = 1; i <= n; i++){sum += abs(cntA[i] - dstA[i]);}minn = min(minn, sum);// 输出两种方案中的最小移动距离cout << minn << endl;return 0;
}

【运行结果】

3
AABBBA
2

D - RLE Moving

【题目来源】

洛谷:[AT_abc421_d ABC421D] RLE Moving - 洛谷

【题目描述】

There is an infinitely large grid. One cell of the grid is named cell \((0,0)\).
有一个无限大的网格。其中一个单元格被命名为单元格 \((0,0)\)

The cell located \(r\) cells down and \(c\) cells right from cell \((0,0)\) is called cell \((r,c)\).
位于单元格 \((0,0)\) 向下 \(r\) 个单元格、向右 \(c\) 个单元格位置的单元格称为单元格 \((r,c)\)

Here, "\(r\) cells down" means "\(|r|\) cells up" when \(r\) is negative, and "\(c\) cells right" means "\(|c|\) cells left" when \(c\) is negative.
此处,"向下 \(r\) 个单元格"在 \(r\) 为负数时表示"向上 \(|r|\) 个单元格","向右 \(c\) 个单元格"在 \(c\) 为负数时表示"向左 \(|c|\) 个单元格"。

Specifically, the cells around cell \((0,0)\) are as follows:
具体来说,单元格 \((0,0)\) 周围的单元格分布如下:

在这里插入图片描述

Initially, Takahashi is at cell \((R_t,C_t)\) and Aoki is at cell \((R_a,C_a)\). They will each make \(N\) moves according to strings \(S\) and \(T\) of length \(N\) consisting of U, D, L, R.
初始时,高桥位于单元格 \((R_t,C_t)\),青木位于单元格 \((R_a,C_a)\)。两人将分别根据长度为 \(N\)、由\(U\)\(D\)\(L\)\(R\)组成的字符串 \(S\)\(T\) 各进行 \(N\) 次移动。

For each \(i\), Takahashi's and Aoki's \(i\)-th moves occur simultaneously: Takahashi moves one cell up if the \(i\)-th character of \(S\) is U, down if D, left if L, and right if R; Aoki moves similarly according to the \(i\)-th character of \(T\).
对于每个 \(i\),高桥和青木的第 \(i\) 次移动同时进行:若 \(S\) 的第 \(i\) 个字符为 \(U\),高桥向上移动一格;为 \(D\) 则向下;为 \(L\) 则向左;为 \(R\) 则向右。青木根据 \(T\) 的第 \(i\) 个字符进行类似移动。

Find the number of times Takahashi and Aoki are at the same cell immediately after a move during the \(N\) moves.
求在 \(N\) 次移动过程中,高桥和青木在移动后立即处于同一单元格的次数。

Since \(N\) is very large, \(S\) and \(T\) are given in the form \(((S'_1, A_1),\ldots,(S'_M,A_M))\) and \(((T'_1,B_1),\ldots,(T'_L,B_L))\), where \(S\) is the string obtained by concatenating "\(A_1\) copies of character \(S'_1\), \(\dots\), \(A_M\) copies of character \(S'_M\)" in this order, and \(T\) is given similarly.
由于 \(N\) 非常大,字符串 \(S\)\(T\)\(((S'_1, A_1),\ldots,(S'_M,A_M))\)\(((T'_1,B_1),\ldots,(T'_L,B_L))\) 的形式给出,其中 \(S\) 是通过按顺序连接"\(A_1\)个字符\(S_1'\)、...、\(A_M\)个字符 \(S_M'\)"得到的字符串, \(T\) 的给出方式类似。

【输入】

The input is given from Standard Input in the following format:

R_t C_t R_a C_a
N M L
S_1' A_1
.
.
.
S_M' A_M
T_1' B_1
.
.
.
T_L' B_L

【输出】

Print the answer.

【输入样例】

0 0 4 2
3 2 1
R 2
D 1
U 3

【输出样例】

1

【算法标签】

《洛谷 AT_abc421_d RLE Moving》 #模拟# #分类讨论#

【代码详解】

#include <bits/stdc++.h>
using namespace std;#define int long long  // 定义int为long long类型,防止整数溢出
const int N = 100005;  // 定义数组最大长度int n, m, l, ans;      // n: 未使用,m: 第一个序列长度,l: 第二个序列长度,ans: 结果计数器
int Rt, Ct, Ra, Ca;    // Rt,Ct: 第一个点的初始位置,Ra,Ca: 第二个点的初始位置
char s[N], t[N];       // s: 第一个序列的方向,t: 第二个序列的方向
int a[N], b[N];        // a: 第一个序列的步长,b: 第二个序列的步长signed main()
{// 输入初始位置cin >> Rt >> Ct >> Ra >> Ca;// 输入两个序列的长度cin >> n >> m >> l;// 输入第一个序列(方向+步长)for (int i = 1; i <= m; i++){cin >> s[i] >> a[i];}// 输入第二个序列(方向+步长)for (int i = 1; i <= l; i++){cin >> t[i] >> b[i];}// 双指针遍历两个序列int i = 1, j = 1;while (i <= m && j <= l){// 取当前两个序列的最小步长int len = min(a[i], b[j]);a[i] -= len;b[j] -= len;// 计算第一个点的移动方向int d1x = 0, d1y = 0;if (s[i] == 'R') d1y = 1;else if (s[i] == 'L') d1y = -1;else if (s[i] == 'U') d1x = -1;else d1x = 1;  // 'D'// 计算第二个点的移动方向int d2x = 0, d2y = 0;if (t[j] == 'R') d2y = 1;else if (t[j] == 'L') d2y = -1;else if (t[j] == 'U') d2x = -1;else d2x = 1;  // 'D'// 判断是否相遇if (s[i] == t[j])  // 方向相同{if (Rt == Ra && Ct == Ca)  // 如果初始位置相同{ans += len;  // 整个移动过程中都在一起}}else  // 方向不同{int R = Rt - Ra;  // 行坐标差int C = Ct - Ca;  // 列坐标差int dx = d2x - d1x;  // 行方向差int dy = d2y - d1y;  // 列方向差int step = -1;if (R || C)  // 如果初始位置不同(即固定A点后,B点不在原点){// 计算相遇需要的步数if (dx != 0) step = R / dx;if (dy != 0) step = C / dy;// 检查是否会在当前段内相遇if (step > 0 && step <= len && dx * step == R && dy * step == C){ans++;}}}// 更新两个点的位置Rt += d1x * len;Ct += d1y * len;Ra += d2x * len;Ca += d2y * len;// 如果当前段走完,移动指针if (a[i] == 0) i++;if (b[j] == 0) j++;}// 输出相遇次数cout << ans << endl;return 0;
}

【运行结果】

0 0 4 2
3 2 1
R 2
D 1
U 3
1

E - Yacht

【题目来源】

洛谷:[AT_abc421_e ABC421E] Yacht - 洛谷

【题目描述】

There are five six-sided dice. Each die has the numbers \(A_1,\ldots,A_6\) written on its faces, and each face appears with probability \(\frac{1}{6}\).
有五枚六面骰子。每枚骰子的六个面上分别写有数字 \(A_1,\ldots,A_6\),每个面出现的概率均为 \(\frac{1}{6}\)

You will play a single-player game using these dice with the following procedure:
您将使用这些骰子按照以下流程进行单人游戏:

  1. Roll all five dice, observe the results, and keep any number (possibly zero) of dice.
    掷出全部五枚骰子,观察结果,并保留任意数量(可能为零)的骰子。
  2. Re-roll all dice that are not kept, observe the results, and additionally keep any number (possibly zero) of the re-rolled dice. The dice kept in the previous step remain kept.
    重新投掷所有未保留的骰子,观察结果,并额外保留任意数量(可能为零)的重新投掷的骰子。上一步已保留的骰子保持保留状态。
  3. Re-roll all dice that are not kept and observe the results.
    重新投掷所有未保留的骰子并观察结果。
  4. Choose any number \(X\). Let \(n\) be the number of dice among the five dice that show \(X\). The score of this game is \(nX\) points.
    选择任意一个数字 \(X\)。令 \(n\) 为五枚骰子中显示 \(X\) 的骰子数量。本游戏的得分为 \(nX\) 分。

Find the expected value of the game score when you act to maximize the expected value of the game score.
求当您采取行动以最大化游戏得分的期望值时,该游戏得分的期望值。

【输入】

The input is given from Standard Input in the following format:

A_1 A_2 A_3 A_4 A_5 A_6

【输出】

Print the answer. Your answer will be considered correct if the relative or absolute error from the true value is at most \(10^{-5}\).

【输入样例】

1 2 3 4 5 6

【输出样例】

14.6588633742

【算法标签】

《洛谷 AT_abc42_e Yacht》 #动态规划DP# #搜索# #记忆化搜索# #期望#

【代码详解】

#include <bits/stdc++.h>
using namespace std;// 预计算6的幂次方数组(0-5次方)
const int pw[6] = {1, 6, 36, 216, 1296, 7776};// 定义DP状态数组:
// f[k][n][S] 表示在第k步,已选n个骰子,状态为S时的期望最大值
double f[4][6][7776];int A[6];  // 存储每个骰子的面值int main()
{// 输入6个骰子的面值for (int i = 0; i < 6; i++){cin >> A[i];}// 初始化第一步(k=1)的DP值for (int n = 0; n <= 5; n++)  // 已选骰子数量(0-5){for (int S = 0; S < pw[n]; S++)  // 遍历所有可能的状态{// 解码当前状态S,统计各面值出现次数int tmp = S, cnt[6] = {0};for (int j = 0; j < n; j++){cnt[tmp % 6]++;tmp /= 6;}// 遍历所有可能的未选骰子组合for (int P = 0; P < pw[5 - n]; P++){tmp = P;int c[6]; memcpy(c, cnt, sizeof(c));  // 复制当前计数// 解码未选骰子的状态for (int j = 0; j < 5 - n; j++){c[tmp % 6]++;tmp /= 6;}// 计算当前得分int score = 0;for (int i = 0; i < 6; i++){int tot = 0;for (int j = 0; j < 6; j++){if (A[j] == A[i]){tot += c[j];}}score = max(score, A[i] * tot);}f[1][n][S] += score;}// 计算期望值f[1][n][S] /= pw[5 - n];}}// 计算第二步和第三步的DP值(k=2,3)for (int k = 2; k <= 3; k++){for (int n = 0; n <= 5; n++){for (int S = 0; S < pw[n]; S++){for (int P = 0; P < pw[5 - n]; P++){double maxv = 0;// 遍历所有可能的保留骰子组合for (int K = 0; K < (1 << (5 - n)); K++){int m = __builtin_popcount(K);  // 计算保留骰子数量int tmp = P, T = S;// 构建新的状态Tfor (int i = 0; i < 5 - n; i++){if (K >> i & 1){T = T * 6 + (tmp % 6);}tmp /= 6;}maxv = max(maxv, f[k - 1][n + m][T]);}f[k][n][S] += maxv;}// 计算期望值f[k][n][S] /= pw[5 - n];}}}// 输出最终结果(保留10位小数)printf("%.10lf\n", f[3][0][0]);return 0;
}

【运行结果】

1 2 3 4 5 6
14.6588633742
http://www.jsqmd.com/news/378934/

相关文章:

  • Nodejs+vue+ElementUI的万事屋智能服务平台的 商城 商家 优惠卷8m7g6296express-mysql
  • 交稿前一晚!AI论文软件 千笔·专业论文写作工具 VS Checkjie,研究生必备神器!
  • Python 偏函数实战指南:用 functools.partial 让代码更优雅
  • 2026年靠谱的化妆品包装/电子包装哪家强生产厂家实力参考 - 行业平台推荐
  • 深入解析Linux内核电源管理:从CPUFreq到SoC挂起的全链路剖析
  • 导师推荐!AI论文网站 千笔·专业论文写作工具 VS 灵感ai,研究生专属神器!
  • 教育平台富文本编辑器粘贴Excel表格是否自动适配?
  • PNG 转 WebP 在线工具哪个好?几款实用网站对比推荐
  • bingle,一个小型搜索引擎项目第一阶段:根本实现(使用Trae自动编程)
  • 2026年质量好的扬州静音脚轮/扬州无磁脚轮实力厂家口碑参考口碑排行 - 行业平台推荐
  • 2026年知名的直线型堆垛机/Miniload堆垛机供应商推荐怎么联系(畅销) - 行业平台推荐
  • 2026年不可错过的电子取证源头厂家口碑推荐排行,光盘抛光修复工具/介质预检恢复取证工作台,电子取证实力厂家怎么选择 - 品牌推荐师
  • 跨平台网页编辑器处理PPT转存格式兼容性如何?
  • 时序数据库 TimechoDB V2.0.8 发布 | 新增 Object 数据类型、协变量预测等功能
  • 搭建k3s,一个轻量级的 Kubernetes 发行版
  • 2026年质量好的高压电缆/低压电缆源头厂家推荐帮我推荐几家 - 行业平台推荐
  • 2026年比较好的5寸脚轮/8寸脚轮生产商推荐怎么选(可靠) - 行业平台推荐
  • 2026国内最新MS胶品牌top5推荐!服务深度覆盖江苏、山东、济南、云南等地,优质MS胶企业权威榜单发布,合规环保助力多场景粘接需求 - 品牌推荐2026
  • 大模型架构演进:从Transformer到MoE
  • Java 大视界 -- Java+Spark 构建离线数据仓库:分层设计与 ETL 研发实战(445)
  • 2026年2月成都装修公司口碑十大推荐榜单,权威评测助您避坑 - 推荐官
  • 2026别错过!千笔,顶流之选的降AIGC网站
  • # 从0开始学习markdown!
  • 深入解析:【Docker入门】容器技术
  • 营区重大活动安保态势三维可视化与指挥调度联动场景——三维空间孪生 × 全域无感定位 × 人车态势融合 × 军用级指挥联动平台
  • 2026水性香薰精油测评:这家ODM公司的精油有何亮点,藤条精油/纳米香氛/藤条香氛/洗手间香薰,精油OEM产品口碑排行 - 品牌推荐师
  • 2026年评价高的全自动立体库/立体库口碑排行实力厂家口碑参考 - 行业平台推荐
  • 2026年评价高的瓶盖高速注塑机/光学透镜高速注塑机最新TOP厂家排名 - 行业平台推荐
  • 2026年质量好的护套控制电缆/软芯控制电缆怎么选真实参考销售厂家参考 - 行业平台推荐
  • AtCoder Beginner Contest竞赛题解 | ABC 422