A14231 | The Sports Festival
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
The student council is preparing for the relay race at the sports festival.
The council consists of $n$ members. They will run one after the other in the race, the speed of member $i$ is $s_i$ . The discrepancy $d_i$ of the $i$ -th stage is the difference between the maximum and the minimum running speed among the first $i$ members who ran. Formally, if $a_i$ denotes the speed of the $i$ -th member who participated in the race, then $d_i = \max(a_1, a_2, \dots, a_i) - \min(a_1, a_2, \dots, a_i)$ .
You want to minimize the sum of the discrepancies $d_1 + d_2 + \dots + d_n$ . To do this, you are allowed to change the order in which the members run. What is the minimum possible sum that can be achieved?
The council consists of $n$ members. They will run one after the other in the race, the speed of member $i$ is $s_i$ . The discrepancy $d_i$ of the $i$ -th stage is the difference between the maximum and the minimum running speed among the first $i$ members who ran. Formally, if $a_i$ denotes the speed of the $i$ -th member who participated in the race, then $d_i = \max(a_1, a_2, \dots, a_i) - \min(a_1, a_2, \dots, a_i)$ .
You want to minimize the sum of the discrepancies $d_1 + d_2 + \dots + d_n$ . To do this, you are allowed to change the order in which the members run. What is the minimum possible sum that can be achieved?
输入格式
The first line contains a single integer $n$ ( $1 \le n \le 2000$ ) — the number of members of the student council.
The second line contains $n$ integers $s_1, s_2, \dots, s_n$ ( $1 \le s_i \le 10^9$ ) – the running speeds of the members.
The second line contains $n$ integers $s_1, s_2, \dots, s_n$ ( $1 \le s_i \le 10^9$ ) – the running speeds of the members.
输出格式
Print a single integer — the minimum possible value of $d_1 + d_2 + \dots + d_n$ after choosing the order of the members.
输入输出样例
输入 #1
3 3 1 2
输出 #1
3
输入 #2
1 5
输出 #2
0
输入 #3
6 1 6 3 3 6 3
输出 #3
11
输入 #4
6 104 943872923 6589 889921234 1000000000 69
输出 #4
2833800505
In the first test case, we may choose to make the third member run first, followed by the first member, and finally the second. Thus $a_1 = 2$ , $a_2 = 3$ , and $a_3 = 1$ . We have:
- $d_1 = \max(2) - \min(2) = 2 - 2 = 0$ .
- $d_2 = \max(2, 3) - \min(2, 3) = 3 - 2 = 1$ .
- $d_3 = \max(2, 3, 1) - \min(2, 3, 1) = 3 - 1 = 2$ .
The resulting sum is $d_1 + d_2 + d_3 = 0 + 1 + 2 = 3$ . It can be shown that it is impossible to achieve a smaller value.
In the second test case, the only possible rearrangement gives $d_1 = 0$ , so the minimum possible result is $0$ .
- $d_1 = \max(2) - \min(2) = 2 - 2 = 0$ .
- $d_2 = \max(2, 3) - \min(2, 3) = 3 - 2 = 1$ .
- $d_3 = \max(2, 3, 1) - \min(2, 3, 1) = 3 - 1 = 2$ .
The resulting sum is $d_1 + d_2 + d_3 = 0 + 1 + 2 = 3$ . It can be shown that it is impossible to achieve a smaller value.
In the second test case, the only possible rearrangement gives $d_1 = 0$ , so the minimum possible result is $0$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted