AtCoder Weekday Contest 0010 Beta题解(AWC 0010 Beta A-E)
A - Distributing Requests to Servers
【题目来源】
AtCoder:A - Distributing Requests to Servers
【题目描述】
Takahashi is a web service operations manager. His system has \(N\) servers, and a large-scale campaign spanning \(M\) days starts tomorrow.
高桥是一名网络服务运维经理。他的系统有 \(N\) 台服务器,一场持续 \(M\) 天的大规模活动将于明天开始。
During the campaign period, exactly \(1\) heavy processing request occurs each day. For each of the \(M\) days, Takahashi must assign that request to exactly \(1\) of the \(N\) servers. The same server may be chosen on multiple days.
在活动期间,每天恰好发生 \(1\) 个重处理请求。对于这 \(M\) 天中的每一天,高桥必须将该请求分配给 \(N\) 台服务器中的恰好 \(1\) 台。同一台服务器可在多天被选择。
Each server \(i\) (\(1 \leq i \leq N\)) has a "processing capacity" \(A_i\). If the total number of requests assigned to server \(i\) throughout the entire campaign period is at most \(A_i\), that server operates normally. On the other hand, if more than \(A_i\) requests are assigned to it, that server will go down. Takahashi must not let any server go down.
每台服务器 \(i\)(\(1 ≤ i ≤ N\))具有一个"处理容量" \(A_i\)。如果分配给服务器 \(i\) 的请求总数在整个活动期间不超过 \(A_i\),则该服务器正常运行。反之,如果分配给它的请求超过 \(A_i\),则该服务器将宕机。高桥绝不能允许任何服务器宕机。
Can Takahashi choose request assignments wisely enough to get through the \(M\)-day campaign without any server going down?
高桥能否明智地选择请求分配方案,以便在 \(M\) 天的活动期间不让任何服务器宕机?
If there exists an assignment that allows all \(M\) days to be completed without any server going down, output Yes; otherwise, output No.
如果存在一种分配方案使得所有 \(M\) 天都能完成且不让任何服务器宕机,则输出 Yes;否则,输出 No。
【输入】
\(N\) \(M\)
\(A_1\) \(A_2\) \(\ldots\) \(A_N\)
- The first line contains an integer \(N\) representing the number of servers and an integer \(M\) representing the number of campaign days, separated by a space.
- The second line contains \(N\) integers \(A_1, A_2, \ldots, A_N\) representing the processing capacity of each server, separated by spaces.
【输出】
If there exists an assignment that allows all \(M\) days of the campaign to be completed without any server going down, output Yes; otherwise, output No on a single line.
【输入样例】
3 10
3 4 2
【输出样例】
No
【解题思路】

【代码详解】
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, m, sum; // n: 数字个数,m: 目标值,sum: 数字总和
signed main()
{
cin >> n >> m; // 读入数字个数n和目标值m
for (int i = 1; i <= n; i++) // 读取n个数字
{
int x;
cin >> x; // 读入第i个数字
sum += x; // 累加到总和
}
if (sum >= m) // 如果总和大于等于目标值
{
cout << "Yes";
}
else
{
cout << "No";
}
return 0;
}
【运行结果】
3 10
3 4 2
No
B - Piano Practice
【题目来源】
AtCoder:B - Piano Practice
【题目描述】
Takahashi aspires to become a pianist, and today he has decided to play \(N\) practice pieces in order from the \(1\)-st to the \(N\)-th.
高桥立志成为一名钢琴家,今天他决定按顺序从第 \(1\) 首到第 \(N\) 首练习 \(N\) 首曲目。
The \(i\)-th piece has an integer difficulty \(D_i\). The time required to perform each piece is determined by the following rules:
第 \(i\) 首曲目有一个整数难度 \(D_i\)。演奏每首曲目所需的时间由以下规则决定:
- The \(1\)-st piece takes \(D_1\) minutes to perform.
第 \(1\) 首曲目需要 \(D_1\) 分钟演奏。 - For the \(2\)-nd piece onward (\(2 \leq i \leq N\)), the performance time depends on the relationship between the difficulty of the current piece and the immediately preceding piece.
从第 \(2\) 首曲目开始(\(2 ≤ i ≤ N\)),演奏时间取决于当前曲目难度与前一曲目难度的关系。 - If the difficulty is strictly greater than the previous piece (\(D_{i-1} < D_i\)), then the previous piece served as a warm-up, so the time is reduced to \(\lfloor D_i / 2 \rfloor\) minutes. Here, \(\lfloor x \rfloor\) denotes the largest integer not exceeding \(x\) (that is, the value obtained by dividing \(D_i\) by \(2\) and rounding down).
如果难度严格大于前一曲目(\(D_{i-1} < D_i\)),则前一曲目起到了热身作用,因此时间减少到 \(⌊D_i/2⌋\) 分钟。此处,\(⌊x⌋\) 表示不超过 \(x\) 的最大整数(即 \(D_i\) 除以 \(2\) 后向下取整得到的值)。 - If the difficulty is less than or equal to the previous piece (\(D_{i-1} \geq D_i\)), no reduction is applied and the piece takes \(D_i\) minutes.
如果难度小于或等于前一曲目(\(D_{i-1} ≥ D_i\)),则不进行任何减少,该曲目需要 \(D_i\) 分钟。
When Takahashi performs all pieces in order from the \(1\)-st to the \(N\)-th, how many minutes does it take in total?
当高桥按顺序从第 \(1\) 首到第 \(N\) 首演奏所有曲目时,总共需要多少分钟?
【输入】
\(N\)
\(D_1\) \(D_2\) \(\ldots\) \(D_N\)
- The first line contains an integer \(N\) representing the number of pieces.
- The second line contains \(N\) integers \(D_1, D_2, \ldots, D_N\) separated by spaces, representing the difficulty of each piece.
【输出】
Print in one line the total time (in minutes) it takes for Takahashi to perform all the pieces.
【输入样例】
5
3 5 4 8 10
【输出样例】
18
【解题思路】

【代码详解】
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 200005;
int n, d[N], ans; // n: 练习曲数量,d[i]: 第i首曲子的练习次数,ans: 总练习时间
signed main()
{
cin >> n; // 读入练习曲数量
for (int i = 1; i <= n; i++)
{
cin >> d[i]; // 读入每首曲子的练习次数
}
ans += d[1]; // 第一首曲子的练习时间
for (int i = 2; i <= n; i++) // 从第二首曲子开始处理
{
if (d[i - 1] >= d[i]) // 如果前一首的练习次数大于等于当前首
{
ans += d[i]; // 加上完整的练习时间
}
else
{
ans += d[i] / 2; // 只加上一半的练习时间
}
}
cout << ans; // 输出总练习时间
return 0;
}
【运行结果】
5
3 5 4 8 10
18
C - Mountain Hut Trails
【题目来源】
AtCoder:C - Mountain Hut Trails
【题目描述】
Takahashi is a mountain guide. In a certain mountainous region, \(N\) mountain huts are lined up in a row. The \(i\)-th mountain hut from the left (hereafter called hut \(i\)) is at an elevation of \(A_i\) meters.
高桥是一名山地导游。在某个山区,\(N\) 个山间小屋排成一行。从左数第 \(i\) 个山间小屋(以下称为小屋 \(i\))的海拔为 \(A_i\) 米。
Between adjacent mountain huts, trails are maintained only when the elevation difference is small. Specifically, for hut \(i\) and hut \(i+1\), if the absolute difference in elevation is at most \(K\) (that is, \(|A_i - A_{i+1}| \leq K\)), there is a trail between them and one can travel directly between them. If the absolute difference in elevation is greater than \(K\), there is no trail and one cannot travel directly between them. There are no trails between non-adjacent mountain huts.
在相邻的山间小屋之间,仅当海拔差异较小时才维护着小径。具体而言,对于小屋 \(i\) 和小屋 \(i+1\),如果海拔差的绝对值至多为 \(K\)(即 \(|A_i - A_{i+1}| ≤ K\)),则它们之间有一条小径,人们可以直接在两者之间通行。如果海拔差的绝对值大于 \(K\),则没有小径,人们不能直接在两者之间通行。非相邻的山间小屋之间没有小径。
A maximal set of mountain huts that can be reached from one another by following a sequence of trails is called an area. Since trails only exist between adjacent huts, the hut numbers belonging to each area always form a contiguous interval. Formally, an area is a contiguous interval \([l, r]\) (\(1 \leq l \leq r \leq N\)) of hut numbers that satisfies both of the following conditions:
一个最大的山间小屋集合,其中任意两个小屋可以通过沿着小径序列互相到达,称为一个区域。由于小径仅存在于相邻小屋之间,每个区域所包含的小屋编号始终构成一个连续区间。形式化地说,一个区域是满足以下两个条件的小屋编号连续区间 \([l, r]\)(\(1 ≤ l ≤ r ≤ N\)):
- There is a trail between all adjacent huts within the interval. That is, for all integers \(i\) such that \(l \leq i \leq r-1\), \(|A_i - A_{i+1}| \leq K\) holds. (When \(l = r\), this condition is automatically satisfied.)
区间内所有相邻小屋之间都有小径。也就是说,对于所有满足 \(l ≤ i ≤ r-1\) 的整数 \(i\),\(|A_i - A_{i+1}| ≤ K\) 成立。(当 \(l = r\) 时,此条件自动满足。) - The interval is maximal. That is, if \(l \geq 2\) then \(|A_{l-1} - A_l| > K\), and if \(r \leq N-1\) then \(|A_r - A_{r+1}| > K\).
该区间是极大的。也就是说,如果 \(l ≥ 2\) 则 \(|A_{l-1} - A_l| > K\) , 且如果 \(r ≤ N-1\) 则 \(|A_r - A_{r+1}| > K\)。
Each of the \(N\) mountain huts belongs to exactly one area.
每个 \(N\) 个山间小屋中的每一个都属于恰好一个区域。
Takahashi wants to answer \(Q\) questions. For the \(j\)-th question, determine whether hut \(L_j\) and hut \(R_j\) belong to the same area.
高桥想要回答 \(Q\) 个问题。对于第 \(j\) 个问题,判断小屋 \(L_j\) 和小屋 \(R_j\) 是否属于同一个区域。
【输入】
\(N\) \(K\) \(Q\)
\(A_1\) \(A_2\) \(\ldots\) \(A_N\)
\(L_1\) \(R_1\)
\(L_2\) \(R_2\)
\(\vdots\)
\(L_Q\) \(R_Q\)
- The first line contains three space-separated integers: \(N\) representing the number of mountain huts, \(K\) representing the maximum elevation difference for a trail to exist (a trail exists when the elevation difference is at most \(K\)), and \(Q\) representing the number of questions.
- The second line contains space-separated integers \(A_1, A_2, \ldots, A_N\) representing the elevations of the mountain huts from left to right.
- The following \(Q\) lines each contain a question.
- The \(j\)-th of these lines (the \((2 + j)\)-th line of the entire input) contains space-separated hut numbers \(L_j\) and \(R_j\) for the \(j\)-th question.
【输出】
Output \(Q\) lines. On the \(j\)-th line, print Yes if hut \(L_j\) and hut \(R_j\) belong to the same area for the \(j\)-th question, and No otherwise.
【输入样例】
6 3 3
10 13 15 25 27 26
1 3
2 5
4 6
【输出样例】
Yes
No
Yes
【解题思路】

【代码详解】
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, k, q; // n: 数组长度,k: 阈值,q: 查询次数
int a[N], b[N]; // a: 原始数组,b: 分组编号数组
int main()
{
cin >> n >> k >> q; // 读入数组长度、阈值和查询次数
for (int i = 1; i <= n; i++)
{
cin >> a[i]; // 读入原始数组
}
int cnt = 1; // 分组计数器,从1开始
b[1] = cnt; // 第一个元素属于第一组
for (int i = 2; i <= n; i++) // 从第二个元素开始处理
{
if (abs(a[i] - a[i - 1]) <= k) // 如果当前元素与前一个元素的差不超过阈值k
{
b[i] = cnt; // 属于同一组
}
else
{
cnt++; // 开始新的组
b[i] = cnt; // 分配新的组编号
}
}
// 调试输出分组编号
// for (int i=1; i<=n; i++)
// cout << b[i] << " ";
// cout << endl;
while (q--) // 处理每个查询
{
int l, r;
cin >> l >> r; // 读入查询的左右边界
if (b[l] == b[r]) // 如果l和r在同一个组
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}
}
return 0;
}
【运行结果】
6 3 3
10 13 15 25 27 26
1 3
Yes
2 5
No
4 6
Yes
D - Rock Destruction
【题目来源】
AtCoder:D - Rock Destruction
【题目描述】
Takahashi works at a quarry. There are \(N\) rocks lined up in a row at the quarry, and he needs to destroy all of them.
高桥在一家采石场工作。采石场有 \(N\) 块岩石排成一行,他需要摧毁所有这些岩石。
The \(i\)-th rock has hardness \(H_i\). Takahashi processes the rocks one by one in order from the \(1\)-st rock to the \(N\)-th rock. He cannot move on to processing the next rock until the current rock is destroyed.
第 \(i\) 块岩石的硬度为 \(H_i\)。高桥按顺序从第 \(1\) 块岩石到第 \(N\) 块岩石逐一处理。在当前岩石被摧毁之前,他不能开始处理下一块岩石。
Each turn, Takahashi chooses and performs exactly one of the following two actions:
每个回合,高桥选择并执行以下两个行动中的恰好一个:
- Strike with a hammer: Reduce the hardness of the rock currently being processed by \(1\). This consumes \(1\) turn.
用锤子敲击:将当前正在处理的岩石的硬度减少 \(1\)。这消耗 \(1\) 个回合。 - Use dynamite: Instantly reduce the hardness of the rock currently being processed to \(0\). This consumes \(1\) turn. However, Takahashi only has \(K\) sticks of dynamite in total, and each use consumes \(1\) stick. This action cannot be chosen when he has \(0\) sticks remaining.
使用炸弹:立即将当前正在处理的岩石的硬度降至 \(0\)。这消耗 \(1\) 个回合。然而,高桥总共只有 \(K\) 根炸弹,每次使用消耗 \(1\) 根。当剩余炸弹为 \(0\) 根时,不能选择此行动。
When a rock's hardness becomes \(0\), that rock is destroyed, and he moves on to processing the next rock. The work is complete once all rocks are destroyed.
当一块岩石的硬度变为 \(0\) 时,该岩石被摧毁,他继续处理下一块岩石。一旦所有岩石都被摧毁,工作就完成了。
Find the minimum number of turns required to destroy all rocks when Takahashi acts optimally.
求高桥最优行动时摧毁所有岩石所需的最少回合数。
【输入】
\(N\) \(K\)
\(H_1\) \(H_2\) \(\ldots\) \(H_N\)
- The first line contains an integer \(N\) representing the number of rocks and an integer \(K\) representing the number of dynamite sticks, separated by a space.
- The second line contains integers \(H_1, H_2, \ldots, H_N\) representing the hardness of each rock, separated by spaces.
【输出】
Print the minimum number of turns required to destroy all rocks in a single line.
【输入样例】
3 1
5 2 8
【输出样例】
8
【解题思路】

【代码详解】
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 200005;
int n, k; // n: 数组长度,k: 前k个元素
int h[N], ans; // h: 原始数组,ans: 结果
signed main()
{
cin >> n >> k; // 读入数组长度和前k个元素的数量
for (int i = 1; i <= n; i++)
{
cin >> h[i]; // 读入数组元素
}
// 将数组从大到小排序
sort(h + 1, h + n + 1, greater<int>());
ans += k; // 前k个元素每个加1(或者固定加k)
// 从第k+1个元素开始,累加剩余的元素
for (int i = k + 1; i <= n; i++)
{
ans += h[i];
}
cout << ans << endl; // 输出结果
return 0;
}
【运行结果】
3 1
5 2 8
8
E - Exhibition Booth Arrangement
【题目来源】
AtCoder:E - Exhibition Booth Arrangement
【题目描述】
Takahashi is in charge of organizing an exhibition where \(N\) companies are exhibiting. The venue has \(N\) booths arranged in a circle, numbered \(1\) to \(N\) in clockwise order. Due to the circular arrangement, booth \(k\) and booth \(k+1\) (\(1 \leq k \leq N-1\)) are adjacent, and booth \(N\) and booth \(1\) are also adjacent. Therefore, there are exactly \(N\) pairs of adjacent booths.
高桥负责组织一场有 \(N\) 家公司参展的展览。会场有 \(N\) 个展位排成一个圆圈,按顺时针顺序编号为 \(1\) 到 \(N\)。由于是圆形排列,展位 \(k\) 和展位 \(k+1\)(\(1 ≤ k ≤ N-1\))相邻,且展位 \(N\) 和展位 \(1\) 也相邻。因此,恰好有 \(N\) 对相邻展位。
The \(N\) companies are also numbered \(1\) to \(N\), and initially, company \(i\) is placed in booth \(i\) (\(1 \leq i \leq N\)). Each booth contains exactly one company, and each company uses exactly one booth.
这 \(N\) 家公司也编号为 \(1\) 到 \(N\),且初始时,公司 \(i\) 被安排在第 \(i\) 个展位(\(1 ≤ i ≤ N\))。每个展位恰好包含一家公司,每家公司恰好使用一个展位。
At the exhibition, for each pair of adjacent booths, the two companies placed there will hold a collaboration event. The audience-drawing effect when company \(i\) and company \(j\) collaborate is represented by \(C_{i,j}\) (\(C_{i,j} = C_{j,i}\)).
在展览中,对于每对相邻展位,安排在那里的两家公司将举办一场合作活动。当公司 \(i\) 和公司 \(j\) 合作时,其吸引观众的效果用 \(C{i,j}\) 表示(\(C_{i,j} = C_{j,i}\))。
Takahashi can perform the following operation \(0\) or more times, up to \(K\) times: choose two distinct companies and swap the booths they are currently using. There are no restrictions on which two companies are chosen for each operation; a company may be chosen in multiple operations, and the same pair of companies may be chosen multiple times.
高桥可以执行以下操作 \(0\) 次或多次,最多 \(K\) 次:选择两家不同的公司,交换它们当前使用的展位。每次操作选择哪两家公司没有限制;同一家公司可以在多次操作中被选中,同一对公司也可以被多次选择。
Consider the arrangement after all operations are completed. Let \(p_k\) denote the company placed in booth \(k\) after the operations. Then \((p_1, p_2, \ldots, p_N)\) is a permutation of \((1, 2, \ldots, N)\). The total audience-drawing effect over all \(N\) pairs of adjacent booths is
考虑所有操作完成后的安排。令 \(p_k\) 表示操作后安排在第 \(k\) 个展位的公司。则 \((p_1, p_2, …, p_N)\) 是 \((1, 2, …, N)\) 的一个排列。所有 \(N\) 对相邻展位的总吸引观众效果为
Find the maximum value of this total when the operations are chosen optimally.
求当操作被最优选择时,该总和的最大值。
【输入】
\(N\) \(K\)
\(C_{1,1}\) \(C_{1,2}\) \(\ldots\) \(C_{1,N}\)
\(C_{2,1}\) \(C_{2,2}\) \(\ldots\) \(C_{2,N}\)
\(\vdots\)
\(C_{N,1}\) \(C_{N,2}\) \(\ldots\) \(C_{N,N}\)
- The first line contains two space-separated integers: \(N\), the number of companies and booths, and \(K\), the maximum number of operations.
- The following \(N\) lines contain the \(N \times N\) matrix \(C\) representing the audience-drawing effects. The \(i\)-th of these lines (\(1 \leq i \leq N\)) contains the space-separated values \(C_{i,1}, C_{i,2}, \ldots, C_{i,N}\), representing the audience-drawing effects between company \(i\) and each other company.
- It is guaranteed that \(C_{i,j} = C_{j,i}\) and \(C_{i,i} = 0\).
【输出】
Print on a single line the maximum total audience-drawing effect over all pairs of adjacent booths that can be achieved after performing \(0\) or more and at most \(K\) operations.
【输入样例】
3 0
0 10 30
10 0 20
30 20 0
【输出样例】
60
【解题思路】

【代码详解】
#include <bits/stdc++.h>
using namespace std;
const int N = 10;
int n, k, ans = -1e9; // n: 公司数量,k: 最大交换次数,ans: 最大总距离
int a[N], b[N], c[N][N]; // a: 当前排列,b: 备用数组,c: 距离矩阵
bool vis[N]; // 访问标记数组
int main()
{
cin >> n >> k; // 读入公司数量和最大交换次数
// 初始化排列
for (int i = 1; i <= n; i++)
{
a[i] = i; // 初始排列为1,2,3,...,n
b[i] = i; // 备用数组
}
// 读入距离矩阵
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cin >> c[i][j];
}
}
// 全排列枚举所有可能的公司顺序
do
{
int sum = 0; // 当前排列的总距离
memset(vis, 0, sizeof(vis)); // 重置访问标记
int cycles = 0; // 环的数量
// 计算当前排列的环数量
for (int i = 1; i <= n; i++)
{
if (!vis[i])
{
cycles++;
int j = i;
while (!vis[j])
{
vis[j] = true;
j = a[j]; // 从公司i开始,沿着排列找到下一个公司
}
}
}
// 最小交换次数 = n - 环的数量
int min_swaps = n - cycles;
// 如果最小交换次数不超过k
if (min_swaps <= k)
{
// 计算总距离:相邻公司距离之和(构成一个环)
for (int i = 2; i <= n; i++)
{
sum += c[a[i]][a[i - 1]];
}
sum += c[a[n]][a[1]]; // 闭合环的距离
// cout << "sum " << sum << endl; // 调试输出
ans = max(ans, sum); // 更新最大总距离
}
}
while (next_permutation(a + 1, a + n + 1)); // 生成下一个排列
cout << ans << endl; // 输出最大总距离
return 0;
}
【运行结果】
3 0
0 10 30
10 0 20
30 20 0
60
浙公网安备 33010602011771号