AtCoder Weekday Contest 0013 Beta题解(AWC 0013 Beta A-E)
A - Path to the Target Score
【题目来源】
AtCoder:A - Path to the Target Score
【题目描述】
Takahashi is playing an online game. His current score is \(A\) points. To reach the top of the rankings, he needs a score of at least \(B\) points.
高桥正在玩一款在线游戏。他当前的得分是 \(A\) 分。为了达到排行榜顶端,他需要至少 \(B\) 分。
The only way for Takahashi to increase his score is by purchasing "Score Boosters" from the in-game item shop. Each Score Booster costs \(C\) coins, and each one purchased increases his score by \(1\) point. He can purchase any number of Score Boosters, including \(0\).
高桥提高得分的唯一方法是从游戏内物品商店购买"得分加成器"。每个得分加成器售价 \(C\) 枚金币,每购买一个会将他的得分提高 \(1\) 分。他可以购买任意数量的得分加成器,包括 \(0\) 个。
Find the minimum number of coins required to bring Takahashi's score to at least \(B\) points.
求将高桥的得分提高到至少 \(B\) 分所需的最少金币数量。
Note that if his current score is already at least \(B\) points, he does not need to purchase any Score Boosters, so the answer is \(0\).
注意,如果他当前的得分已经至少是 \(B\) 分,他就不需要购买任何得分加成器,因此答案是 \(0\)。
【输入】
\(A\) \(B\) \(C\)
A single line contains Takahashi's current score \(A\), the minimum score \(B\) required to reach the top of the rankings, and the price \(C\) per Score Booster, separated by spaces.
【输出】
Print on a single line the minimum number of coins required to bring Takahashi's score to at least \(B\) points.
Note that the answer may be as large as approximately \(10^{18}\).
【输入样例】
50 100 3
【输出样例】
150
【解题思路】

【代码详解】
#include <bits/stdc++.h>
using namespace std;
#define int long long
int a, b, c; // a: 初始值,b: 目标值,c: 单位成本
signed main()
{
cin >> a >> b >> c; // 读入初始值、目标值和单位成本
if (b >= a) // 如果目标值大于等于初始值
{
cout << (b - a) * c; // 计算差值乘以单位成本
}
else
{
cout << 0; // 如果目标值小于初始值,输出0
}
return 0;
}
【运行结果】
50 100 3
150
B - Investigation of Friend Relationships
【题目来源】
AtCoder:B - Investigation of Friend Relationships
【题目描述】
Takahashi is the student council president of a school. There are \(N\) students in this school, each assigned a student number from \(1\) to \(N\).
高桥是一所学校的学生会长。这所学校有 \(N\) 名学生,每名学生的学号从 \(1\) 到 \(N\) 分配。
This school has a system where students can send "friend requests" to each other. Friend requests are one-directional. That is, a friend request from student \(u\) to student \(v\) and a friend request from student \(v\) to student \(u\) are treated as different requests — it is possible for only one of them to exist, or for both to exist simultaneously.
这所学校有一个学生可以相互发送"好友请求"的系统。好友请求是单向的。也就是说,从学生 \(u\) 到学生 \(v\) 的好友请求与从学生 \(v\) 到学生 \(u\) 的好友请求被视为不同的请求——有可能只存在其中之一,或者两者同时存在。
Currently, \(M\) friend requests have been sent. The \(i\)-th friend request \((1 \leq i \leq M)\) was sent from student \(A_i\) to student \(B_i\).
目前,已经发送了 \(M\) 个好友请求。第 \(i\) 个好友请求(\(1 ≤ i ≤ M\))是从学生 \(A_i\) 发送给学生 \(B_i\) 的。
Takahashi is planning an event for the school festival where students participate in pairs. Only pairs of students who have mutually sent friend requests to each other can participate in this event. Here, students \(u\) and \(v\) "have mutually sent friend requests to each other" means that both a friend request from student \(u\) to student \(v\) and a friend request from student \(v\) to student \(u\) exist.
高桥正在为学校文化节策划一个学生两人一组参加的活动。只有互相发送过好友请求的学生对才能参加这个活动。在这里,学生 \(u\) 和学生 \(v\) "互相发送过好友请求"意味着从学生 \(u\) 到学生 \(v\) 的好友请求和从学生 \(v\) 到学生 \(u\) 的好友请求同时存在。
For Takahashi, find the number of pairs of students who have mutually sent friend requests to each other. Note that the pair of student \(u\) and student \(v\) and the pair of student \(v\) and student \(u\) are considered the same pair and counted as \(1\).
为高桥找出互相发送过好友请求的学生对的数量。注意,学生 \(u\) 和学生 \(v\) 的对与学生 \(v\) 和学生 \(u\) 的对被视为同一对,计为 \(1\)。
【输入】
\(N\) \(M\)
\(A_1\) \(B_1\)
\(A_2\) \(B_2\)
\(\vdots\)
\(A_M\) \(B_M\)
The first line contains the number of students \(N\) and the number of friend requests \(M\), separated by a space.
The \(i\)-th of the following \(M\) lines \((1 \leq i \leq M)\) contains a pair of integers \(A_i, B_i\) separated by a space, representing a friend request from student \(A_i\) to student \(B_i\).
【输出】
Print the number of pairs of students who have mutually sent friend requests to each other, in a single line.
【输入样例】
4 5
1 2
2 1
1 3
3 4
4 3
【输出样例】
2
【解题思路】

【代码详解】
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII; // 定义pair类型
const int N = 100005;
int n, m, ans; // n: 顶点数,m: 边数,ans: 统计结果
set<PII> st; // 存储边的集合
int main()
{
cin >> n >> m; // 读入顶点数和边数
for (int i = 1; i <= m; i++) // 处理每条边
{
int u, v;
cin >> u >> v; // 读入一条边(u, v)
// 检查是否存在反向边(v, u)
if (st.count({v, u}))
{
ans++; // 如果存在,说明找到了一对双向边
}
st.insert({u, v}); // 将当前边加入集合
}
cout << ans; // 输出双向边的数量
return 0;
}
【运行结果】
4 5
1 2
2 1
1 3
3 4
4 3
2
C - Optimal Pairing for a Tournament
【题目来源】
AtCoder:C - Optimal Pairing for a Tournament
【题目描述】
Takahashi is going to organize a chess elimination tournament. \(N\) players participate in this tournament, each numbered from \(1\) to \(N\). Each player \(i\) has a rating \(S_i\), and all players have distinct ratings.
高桥将组织一场国际象棋淘汰赛。\(N\) 名选手参加本次比赛,每位选手编号从 \(1\) 到 \(N\)。每位选手 \(i\) 有一个评分 \(S_i\),且所有选手的评分互不相同。
In the tournament, matches are repeated until only \(1\) player remains. In each match, \(2\) players who have not yet been eliminated are chosen to compete against each other in a one-on-one match. The player with the higher rating always wins, and the losing player is eliminated from the tournament. Therefore, a total of \(N - 1\) matches are held throughout the tournament.
在比赛中,将重复进行比赛直到只剩 \(1\) 名选手。在每场比赛中,选择 \(2\) 名尚未被淘汰的选手进行一对一比赛。评分较高的选手总是获胜,失败的选手从比赛中淘汰。因此,整个比赛将总共进行 \(N - 1\) 场比赛。
Takahashi, as the organizer, is free to decide which \(2\) players compete in each match. Since Takahashi knows all players' ratings in advance and can perfectly predict the outcome of every match, he can optimally plan all match pairings before the tournament begins.
高桥作为组织者,可以自由决定每场比赛由哪 \(2\) 名选手对阵。由于高桥提前知道所有选手的评分,并且可以完美预测每场比赛的结果,他可以在比赛开始前最优地规划所有对阵组合。
Takahashi has a player he is rooting for: player \(K\). Takahashi wants to optimally arrange the match pairings to maximize the number of matches that player \(K\) wins. Player \(K\) will lose and be eliminated if matched against a player with a higher rating, after which their win count can no longer increase.
高桥有一位他支持的选手:选手 \(K\)。高桥希望最优地安排对阵组合,以最大化选手 \(K\) 获胜的比赛场数。如果选手 \(K\) 对阵评分更高的选手,他将输掉比赛并被淘汰,之后他的胜场数将无法再增加。
Determine the maximum number of matches player \(K\) can win.
确定选手 \(K\) 能赢得的最大比赛场数。
【输入】
\(N\) \(K\)
\(S_1\) \(S_2\) \(\ldots\) \(S_N\)
- The first line contains \(N\), the number of players, and \(K\), the number of the player Takahashi is rooting for, separated by a space.
- The second line contains the ratings \(S_1, S_2, \ldots, S_N\) of each player, separated by spaces.
【输出】
Output in one line the maximum number of matches player \(K\) can win.
【输入样例】
4 3
5 1 3 2
【输出样例】
2
【解题思路】

【代码详解】
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, k; // n: 数组长度,k: 指定位置
int s[N], ans; // s: 数组,ans: 统计结果
int main()
{
cin >> n >> k; // 读入数组长度和指定位置
for (int i = 1; i <= n; i++)
{
cin >> s[i]; // 读入数组元素
}
int t = s[k]; // 保存第k个位置的值
sort(s + 1, s + n + 1); // 对整个数组排序
for (int i = 1; i <= n; i++)
{
if (s[i] < t) // 统计比原第k个元素小的元素个数
{
ans++;
}
}
cout << ans; // 输出结果
return 0;
}
【运行结果】
4 3
5 1 3 2
2
D - Distance Between Cities
【题目来源】
AtCoder:D - Distance Between Cities
【题目描述】
Takahashi has been put in charge of urban planning for a certain region. This region has \(N\) cities, each numbered from \(1\) to \(N\).
高桥被委任负责某个地区的城市规划。这个地区有 \(N\) 个城市,每个城市编号从 \(1\) 到 \(N\)。
The position of each city \(i\) (\(1 \leq i \leq N\)) is represented by integer coordinates in \(M\)-dimensional space, where \(A_{i,k}\) denotes the coordinate value in the \(k\)-th dimension (\(1 \leq k \leq M\)).
每个城市 \(i\)(\(1 ≤ i ≤ N\))的位置用 \(M\) 维空间中的整数坐标表示,其中 \(A_{i,k}\) 表示第 \(k\) 维(\(1 ≤ k ≤ M\))的坐标值。
Takahashi defines the distance between two cities using the Manhattan distance. That is, the distance between city \(i\) and city \(j\) is \(\displaystyle\sum_{k=1}^{M} |A_{i,k} - A_{j,k}|\).
高桥使用曼哈顿距离来定义两个城市之间的距离。也就是说,城市 \(i\) 和城市 \(j\) 之间的距离是 \(\displaystyle\sum_{k=1}^{M} |A_{i,k} - A_{j,k}|\).。
Takahashi wants to compute the distances for all unordered pairs of distinct cities and find their total sum.
高桥想要计算所有无序的不同城市对之间的距离,并求它们的总和。
Specifically, compute the following value:
具体而言,计算以下值:
\(\sum_{1 \leq i < j \leq N} \sum_{k=1}^{M} |A_{i,k} - A_{j,k}|\)
【输入】
\(N\) \(M\)
\(A_{1,1}\) \(A_{1,2}\) \(\ldots\) \(A_{1,M}\)
\(A_{2,1}\) \(A_{2,2}\) \(\ldots\) \(A_{2,M}\)
\(\vdots\)
\(A_{N,1}\) \(A_{N,2}\) \(\ldots\) \(A_{N,M}\)
- The first line contains the number of cities \(N\) and the number of dimensions \(M\), separated by a space.
- The \(i\)-th of the following \(N\) lines (\(1 \leq i \leq N\)) contains the \(M\)-dimensional coordinate values of city \(i\): \(A_{i,1}, A_{i,2}, \ldots, A_{i,M}\), separated by spaces.
【输出】
Output the total sum of Manhattan distances over all unordered pairs of distinct cities, as a single integer on one line.
【输入样例】
3 2
1 2
4 6
7 1
【输出样例】
22
【解题思路】

【代码详解】
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 200005;
int n, m, ans; // n: 序列长度,m: 维度/属性数
int a[15][N], sa[15][N]; // a: 原始数据,sa: 前缀和数组
signed main()
{
cin >> n >> m; // 读入序列长度和维度
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> a[j][i]; // 注意:输入存储为a[维度][索引]
}
}
for (int j = 1; j <= m; j++) // 对每个维度分别处理
{
// 对第j维的所有值排序
sort(a[j] + 1, a[j] + n + 1);
// 计算排序后第j维的前缀和
for (int i = 1; i <= n; i++)
{
sa[j][i] = sa[j][i - 1] + a[j][i];
}
// 计算第j维的贡献
for (int i = 2; i <= n; i++) // 注意:从i=2开始
{
// 公式:a[j][i] * (i-1) - sa[j][i-1]
// 这计算的是排序后,a[j][i]与它前面所有元素的差之和
ans += a[j][i] * (i - 1) - sa[j][i - 1];
}
}
cout << ans << endl; // 输出结果
return 0;
}
【运行结果】
3 2
1 2
4 6
7 1
22
浙公网安备 33010602011771号