AtCoder Weekday Contest 0034 Beta题解(AWC 0034 Beta A-E)
A - Reception Processing at the Service Window
【题目来源】
AtCoder:A - Reception Processing at the Service Window
【题目描述】
Takahashi works as a general information clerk at a city hall. At this city hall, applications for procedures flood in at the end of the fiscal year, so efficient counter management is required.
高桥是市政厅的一名综合咨询员。每年财政年度末,市政厅会涌入大量业务申请,因此需要高效地管理服务窗口。
The city hall has \(N\) counters, numbered from \(1\) to \(N\). Each counter \(i\) \((1 \leq i \leq N)\) has a daily reception limit \(C_i\), meaning it can accept up to \(C_i\) applications per day. At the beginning of the day, the number of accepted applications at every counter is \(0\).
市政厅设有 \(N\) 个服务窗口,编号从 \(1\) 到 \(N\)。每个窗口 \(i\)(\(1 \leq i \leq N\))有一个每日受理上限 \(C_i\),表示每天最多可接受 \(C_i\) 份申请。每天开始时,每个窗口已接受的申请数均为 \(0\)。
On a certain day, \(M\) applications arrived in order. The \(j\)-th application \((1 \leq j \leq M)\) is from a citizen who wishes to have their procedure handled at counter \(T_j\).
某一天,按顺序收到了 \(M\) 份申请。第 \(j\) 份申请(\(1 \leq j \leq M\))来自一位希望在窗口 \(T_j\) 办理业务的市民。
The applications are processed one by one in the order \(j = 1, 2, \ldots, M\). Each application \(j\) is processed as follows:
申请按照 \(j = 1, 2, \ldots, M\) 的顺序逐一处理。每份申请 \(j\) 的处理流程如下:
- If the current number of accepted applications at the desired counter \(T_j\) is less than the reception limit \(C_{T_j}\), the application is accepted, and the number of accepted applications at counter \(T_j\) increases by \(1\).
如果所期望的窗口 \(T_j\) 当前已接受的申请数小于受理上限 \(C_{T_j}\),则该申请被接受,窗口 \(T_j\) 的已接受申请数增加 \(1\)。 - If the current number of accepted applications at the desired counter \(T_j\) is equal to the reception limit \(C_{T_j}\) (i.e., the reception limit has been reached), the application is rejected. In the case of rejection, the number of accepted applications does not change, and the application is not redirected to any other counter.
如果所期望的窗口 \(T_j\) 当前已接受的申请数等于受理上限 \(C_{T_j}\)(即已达到受理上限),则该申请被拒绝。拒绝情况下,已接受申请数不发生变化,且该申请不会被转至其他窗口。
After processing all applications, find the total number of accepted applications.
处理完所有申请后,求总共被接受的申请数量。
【输入】
\(N\) \(M\)
\(C_1\) \(C_2\) \(\ldots\) \(C_N\)
\(T_1\)
\(T_2\)
\(\vdots\)
\(T_M\)
- The first line contains two integers separated by a space: \(N\), the number of counters, and \(M\), the number of applications.
- The second line contains \(N\) integers separated by spaces: \(C_1, C_2, \ldots, C_N\), representing the daily reception limit of each counter.
- From the 3rd line to the \((2 + M)\)-th line, each line contains the counter number desired in an application. Specifically, the \((2 + j)\)-th line \((1 \leq j \leq M)\) contains \(T_j\), the counter number desired in the \(j\)-th application.
【输出】
Print the total number of accepted applications in a single line.
【输入样例】
3 5
2 1 3
1
2
1
1
2
【输出样例】
3
【代码详解】
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, m, ans; // n: 盒子数, m: 操作次数, ans: 结果
int a[N], c[N]; // a: 当前球数, c: 容量
int main()
{
cin >> n >> m; // 输入盒子数和操作次数
for (int i = 1; i <= n; i++)
{
cin >> c[i]; // 输入每个盒子的容量
}
while (m--) // 处理每次操作
{
int x;
cin >> x; // 输入要放入的盒子编号
if (a[x] < c[x]) // 如果盒子未满
{
a[x]++; // 放入一个球
ans++; // 成功放入计数
}
}
cout << ans << endl; // 输出成功放入的总球数
return 0;
}
【运行结果】
3 5
2 1 3
1
2
1
1
2
3
B - From Station to Station
【题目来源】
AtCoder:B - From Station to Station
【题目描述】
Takahashi is investigating a railway line. This line has \(N\) stations, numbered from \(1\) to \(N\).
高桥正在调查一条铁路线。这条线上有 \(N\) 个车站,编号从 \(1\) 到 \(N\)。
This line has a special structure. From station \(i\) (\(1 \leq i \leq N-1\)), only a train heading to station \(P_i\) is operated. Here, \(P_i\) is an integer between \(1\) and \(N\), inclusive, that is different from \(i\). Station \(N\) is the final destination, and no trains depart from station \(N\).
这条线路具有特殊结构。从车站 \(i\)(\(1 \leq i \leq N-1\))出发,只运营一班开往车站 \(P_i\) 的列车。这里,\(P_i\) 是一个介于 \(1\) 到 \(N\) 之间的整数,且不等于 \(i\)。车站 \(N\) 是最终目的地,没有列车从车站 \(N\) 出发。
Takahashi starts at station \(1\) and repeatedly boards the only train available at each station, aiming to reach station \(N\). The journey ends when he arrives at station \(N\).
高桥从车站 \(1\) 出发,反复搭乘每个车站唯一可用的列车,旨在到达车站 \(N\)。当他到达车站 \(N\) 时,旅程结束。
The input is given such that starting from station \(1\) and repeating the above process, he will always reach station \(N\) in a finite number of moves without visiting the same station twice.
输入保证从车站 \(1\) 出发并重复上述过程,他总能在有限次移动后到达车站 \(N\),且不会重复访问同一车站。
Find the number of stations visited from station \(1\) until reaching station \(N\). Include both the departure station (station \(1\)) and the arrival station (station \(N\)) in the count.
求从车站 \(1\) 出发直到到达车站 \(N\) 所访问的车站数量。计数时包括始发站(车站 \(1\))和到达站(车站 \(N\))。
【输入】
\(N\)
\(P_1\) \(P_2\) \(\cdots\) \(P_{N-1}\)
- The first line contains an integer \(N\), representing the number of stations.
- The second line contains \(N-1\) integers \(P_1, P_2, \ldots, P_{N-1}\) separated by spaces. \(P_i\) represents the number of the next station reachable by train from station \(i\). Since no trains depart from station \(N\), \(P_N\) is not given.
【输出】
Print in one line the number of stations visited from station \(1\) until reaching station \(N\) (including both station \(1\) and station \(N\)).
【输入样例】
5
2 4 5 5
【输出样例】
4
【代码详解】
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, p[N], cnt = 1; // n: 数组长度, p: 排列数组, cnt: 步数计数器
int main()
{
cin >> n; // 输入数组长度
for (int i = 1; i <= n; i++)
{
cin >> p[i]; // 输入排列
}
int st = 1; // 起始位置
while (st != n) // 当未到达位置n时循环
{
st = p[st]; // 跳转到p[st]位置
cnt++; // 步数加1
}
cout << cnt << endl; // 输出从1到n所需的步数
return 0;
}
【运行结果】
5
2 4 5 5
4
C - Watering the Flower Bed
【题目来源】
AtCoder:C - Watering the Flower Bed
【题目描述】
Takahashi manages \(N\) flower beds arranged in a row in his garden. The flower beds are numbered \(1, 2, \ldots, N\) from left to right, and the current moisture level of the soil in flower bed \(i\) (\(1 \leq i \leq N\)) is \(A_i\).
高桥在他的花园中管理着一排 \(N\) 个花坛。花坛从左到右编号为 \(1, 2, \ldots, N\),花坛 \(i\)(\(1 \leq i \leq N\))土壤当前的湿度水平为 \(A_i\)。
Since he will have guests tomorrow, Takahashi wants to make all flower beds have a moisture level of at least the target value \(T\) so that the flowers bloom beautifully. It is fine if the moisture level exceeds \(T\).
由于明天将有客人来访,高桥希望所有花坛的湿度水平至少达到目标值 \(T\),以便花朵美丽绽放。湿度水平超过 \(T\) 也可以。
Takahashi can perform the following operation any number of times (possibly \(0\) times):
高桥可以执行任意次(包括 \(0\) 次)以下操作:
- Operation: Choose an integer \(l\) satisfying \(1 \leq l \leq N - K + 1\), and water the \(K\) consecutive flower beds from flower bed \(l\) to flower bed \(l + K - 1\) using a sprinkler. This increases the moisture level of each of those \(K\) flower beds by \(1\). Each operation costs \(C\) yen in water charges.
操作:选择一个满足 \(1 \leq l \leq N - K + 1\) 的整数 \(l\),并使用洒水器为从花坛 \(l\) 到花坛 \(l + K - 1\) 的 \(K\) 个连续花坛浇水。这会将这 \(K\) 个花坛的湿度水平各提高 \(1\)。每次操作的水费为 \(C\) 日元。
The value of \(l\) chosen in each operation is arbitrary, and the same value of \(l\) may be chosen in different operations.
每次操作所选的 \(l\) 值可以任意,且在不同操作中可以选择相同的 \(l\) 值。
Find the minimum total water cost (in yen) required to make the moisture level of all flower beds at least \(T\).
求使所有花坛的湿度水平至少达到 \(T\) 所需的最小总水费(以日元计)。
【输入】
\(N\) \(K\) \(T\) \(C\)
\(A_1\) \(A_2\) \(\cdots\) \(A_N\)
- The first line contains the number of flower beds \(N\), the number of flower beds watered simultaneously by the sprinkler \(K\), the target moisture level \(T\), and the water cost per operation \(C\) (yen), separated by spaces.
- The second line contains the initial moisture levels of each flower bed \(A_1, A_2, \ldots, A_N\), separated by spaces.
【输出】
Print the minimum total water cost (in yen) required to make the moisture level of all flower beds at least \(T\), as an integer on a single line.
【输入样例】
5 3 10 100
8 7 9 12 11
【输出样例】
300
【代码详解】
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 200005;
int n, k, t, c, ans; // n: 数组长度, k: 影响范围, t: 目标值, c: 单位代价, ans: 操作次数
int a[N], imos[N], cur; // a: 原始数组, imos: 差分数组, cur: 当前累计增加值
signed main()
{
cin >> n >> k >> t >> c; // 输入参数
for (int i = 1; i <= n; i++)
{
cin >> a[i]; // 输入原始数组
}
for (int i = 1; i <= n; i++) // 遍历每个位置
{
cur += imos[i]; // 更新当前位置的累计增加值
if (a[i] + cur < t) // 如果当前值小于目标值
{
int need = t - (a[i] + cur); // 计算需要增加的值
ans += need; // 累计操作次数
if (i + k <= n) // 如果影响范围未超出数组
{
imos[i + k] -= need; // 在差分数组记录减少
}
cur += need; // 当前累计值增加
}
}
cout << ans * c << endl; // 输出总代价
return 0;
}
【运行结果】
5 3 10 100
8 7 9 12 11
300
浙公网安备 33010602011771号