AtCoder Weekday Contest 0023 Beta题解(AWC 0023 Beta A-E)
A - Chef's Break
【题目来源】
AtCoder:A - Chef's Break
【题目描述】
Takahashi is a chef working in a restaurant kitchen.
高桥是一家餐厅厨房的厨师。
Today, Takahashi needs to prepare \(N\) ingredients. The time required to prepare the \(i\)-th ingredient is \(T_i\) seconds. Takahashi can only prepare one ingredient at a time.
今天,高桥需要准备 \(N\) 种食材。准备第 \(i\) 种食材所需的时间为 \(T_i\) 秒。高桥一次只能准备一种食材。
Additionally, according to the kitchen rules, Takahashi must take exactly \(M\) breaks during his work. Each break takes \(R\) seconds. A break can only be taken immediately after finishing the preparation of any ingredient (breaks cannot be taken before starting the first ingredient or after finishing the last ingredient).
此外,根据厨房规定,高桥在工作期间必须恰好休息 \(M\) 次。每次休息需要 \(R\) 秒。休息只能在完成任意一种食材的准备工作后立即进行(不能在开始第一种食材之前或完成最后一种食材之后休息)。
Find the minimum total time from the start of work to the completion of all work, when preparing all ingredients and taking exactly \(M\) breaks. Note that the order in which ingredients are prepared can be freely chosen.
求从工作开始到完成所有工作的最小总时间,包括准备所有食材和恰好休息 \(M\) 次。注意,准备食材的顺序可以自由选择。
【输入】
\(N\) \(M\) \(R\)
\(T_1\) \(T_2\) \(\ldots\) \(T_N\)
- The first line contains \(N\) representing the number of ingredients, \(M\) representing the number of breaks, and \(R\) representing the time for one break, separated by spaces.
- The second line contains the times \(T_1, T_2, \ldots, T_N\) required to prepare each ingredient, separated by spaces.
【输出】
Output in one line the minimum total time when preparing all ingredients and taking exactly \(M\) breaks.
【输入样例】
3 1 5
10 20 30
【输出样例】
65
【解题思路】

【代码详解】
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, m, r; // n: 数量,m: 最大操作次数,r: 每次操作的增量
int ans; // 最终结果
signed main()
{
cin >> n >> m >> r; // 读入n, m, r
for (int i = 1; i <= n; i++) // 遍历n个元素
{
int x;
cin >> x; // 读入第i个元素的值
ans += x; // 累加到结果中
}
// 可以执行的操作:对n-1个元素增加r
// 但最多执行m次
ans += min(n - 1, m) * r; // 增加额外收益
cout << ans << endl; // 输出最终结果
return 0;
}
【运行结果】
3 1 5
10 20 30
65
B - Bus Tour
【题目来源】
AtCoder:B - Bus Tour
【题目描述】
Takahashi is operating a sightseeing bus tour. In this tour, a bus picks up passengers while visiting multiple stops in order.
高桥正在运营一条观光巴士线路。在这条线路上,巴士按顺序访问多个站点,并在途中接载乘客。
The tour has \(N\) stops, numbered stop \(1\), stop \(2\), ..., stop \(N\) in order from the departure point. Stop \(1\) is the first stop, and stop \(N\) is the final destination.
该线路有 \(N\) 个站点,从起点开始按顺序编号为站点 \(1\)、站点 \(2\)、……、站点 \(N\)。站点 \(1\) 是起点站,站点 \(N\) 是终点站。
The bus is initially empty (before any boarding takes place at stop \(1\)).
巴士初始时为空车(在站点 \(1\) 任何人上车之前)。
At each stop \(i\) (\(1 \leq i \leq N\)), \(A_i\) new passengers board the bus.
在每个站点 \(i\)(\(1 \leq i \leq N\)),有 \(A_i\) 名新乘客上车。
Furthermore, at each stop \(i\) (\(1 \leq i \leq N - 1\)), after boarding, \(B_i\) passengers get off the bus and leave the tour during the trip to the next stop \(i + 1\). However, if the number of passengers currently on the bus is less than \(B_i\), all passengers on the bus get off.
此外,在每个站点 \(i\)(\(1 \leq i \leq N - 1\)),上车之后,在前往下一个站点 \(i + 1\) 的途中,有 \(B_i\) 名乘客下车并离开线路。但是,如果当前巴士上的乘客数量少于 \(B_i\),则巴士上的所有乘客都会下车。
In summary, the process at each stop \(i\) is as follows:
总结来说,在每个站点 \(i\) 的过程如下:
- \(A_i\) new passengers board the bus.
\(A_i\) 名新乘客上车。 - If \(i \leq N - 1\), during the trip to the next stop, \(\min(\text{current number of passengers}, B_i)\) passengers get off. If \(i = N\), no passengers get off.
如果 \(i \leq N - 1\),在前往下一个站点的途中,有 \(\min(\text{当前乘客数量}, B_i)\) 名乘客下车。如果 \(i = N\),则没有乘客下车。
Find the number of passengers on the bus when boarding at stop \(N\) is completed.
求在站点 \(N\) 的上车完成时,巴士上的乘客数量。
【输入】
\(N\)
\(A_1\) \(B_1\)
\(A_2\) \(B_2\)
\(\vdots\)
\(A_{N-1}\) \(B_{N-1}\)
\(A_N\)
- The first line contains an integer \(N\), representing the number of stops.
- The \((i + 1)\)-th line (\(1 \leq i \leq N - 1\)) contains the number of passengers boarding at stop \(i\), \(A_i\), and the number of passengers getting off, \(B_i\), separated by a space.
- The \((N + 1)\)-th line contains only \(A_N\), the number of passengers boarding at stop \(N\).
【输出】
Print on a single line the number of passengers on the bus when boarding at stop \(N\) is completed.
【输入样例】
3
5 2
3 1
4
【输出样例】
9
【解题思路】

【代码详解】
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, a, b, ans; // n: 阶段数,a: 增加的值,b: 减少的值,ans: 当前值
signed main()
{
cin >> n; // 读入总阶段数
for (int i = 1; i < n; i++) // 处理前n-1个阶段
{
cin >> a >> b; // 读入当前阶段的增加值和减少值
ans += a; // 先加上增加值
ans = max(0LL, ans - b); // 减去减少值,但不能小于0
}
// 处理最后一个阶段
cin >> a; // 读入最后一个阶段的增加值
ans += a; // 加上最后一个阶段的增加值
// 注意:最后一个阶段不减去减少值
cout << ans << endl; // 输出最终结果
return 0;
}
【运行结果】
3
5 2
3 1
4
9
C - Factory Tour
【题目来源】
AtCoder:C - Factory Tour
【题目描述】
Takahashi works as a guide for a tour of a large factory.
高桥在一家大型工厂担任导览。
The factory has \(N\) manufacturing areas, each numbered from \(1\) to \(N\). Visiting each area \(i\) takes exactly \(T_i\) minutes.
这家工厂有 \(N\) 个生产区域,每个区域编号从 \(1\) 到 \(N\)。访问每个区域 \(i\) 恰好需要 \(T_i\) 分钟。
Today, \(M\) tour groups will visit the factory. Each group \(j\) arrives at the factory at time \(S_j\) minutes, and immediately after arrival, they visit all areas with numbers from \(L_j\) to \(R_j\). The group visits the areas one by one in order of increasing area number, and as soon as they finish visiting one area, they immediately begin visiting the next area (there is no travel time between areas).
今天,将有 \(M\) 个参观团到访工厂。每个参观团 \(j\) 在 \(S_j\) 分钟时到达工厂,到达后立即开始访问从 \(L_j\) 到 \(R_j\) 编号的所有区域。参观团按区域编号递增的顺序逐一访问各个区域,并且一旦完成对一个区域的访问,就立即开始访问下一个区域(区域之间没有行程时间)。
Each area is spacious enough that even if multiple groups are visiting the same area at the same time, they do not affect each other.
每个区域都足够宽敞,即使多个参观团同时访问同一区域,它们也不会互相影响。
Takahashi wants to calculate in advance the time at which each group finishes all their visits, and inform the groups.
高桥希望提前计算出每个参观团完成所有访问的时间,并告知各个参观团。
For each group, determine the time (in minutes) at which they complete visiting all of their designated areas.
对于每个参观团,确定他们完成所有指定区域访问的时间(以分钟为单位)。
【输入】
\(N\) \(M\)
\(T_1\) \(T_2\) \(\ldots\) \(T_N\)
\(S_1\) \(L_1\) \(R_1\)
\(S_2\) \(L_2\) \(R_2\)
\(\vdots\)
\(S_M\) \(L_M\) \(R_M\)
- The first line contains the number of areas \(N\) and the number of groups \(M\), separated by a space.
- The second line contains the visiting times \(T_1, T_2, \ldots, T_N\) for each area, separated by spaces.
- The following \(M\) lines contain the information for each group.
- The \((2 + j)\)-th line contains the arrival time \(S_j\) of the \(j\)-th group, and the start \(L_j\) and end \(R_j\) of the range of area numbers to visit, separated by spaces.
【输出】
Print \(M\) lines.
The \(j\)-th line should contain the time in minutes at which the \(j\)-th group finishes all their visits.
【输入样例】
5 3
10 20 30 40 50
0 1 3
5 2 4
100 5 5
【输出样例】
60
95
150
【解题思路】

【代码详解】
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 200005;
int n, m; // n: 任务数量,m: 查询次数
int t[N], st[N]; // t[i]: 第i个任务的时间,st[i]: 前缀和数组
signed main()
{
cin >> n >> m; // 读入任务数量和查询次数
for (int i = 1; i <= n; i++)
{
cin >> t[i]; // 读入每个任务需要的时间
st[i] = st[i - 1] + t[i]; // 计算前缀和
}
while (m--) // 处理每个查询
{
int s, l, r;
cin >> s >> l >> r; // 读入起始时间s和区间[l, r]
// 输出从时间s开始,完成区间[l, r]内所有任务后的时间
cout << s + st[r] - st[l - 1] << endl;
}
return 0;
}
【运行结果】
5 3
10 20 30 40 50
0 1 3
60
5 2 4
95
100 5 5
150
浙公网安备 33010602011771号