欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1509C.The Sports Festival
The Sports Festival
CF1509C · The Sports Festival
- 难度:1800
- 标签:dp、greedy
- 链接:https://codeforces.com/problemset/problem/1509/C
- 时间限制:1 second 内存限制:256 megabytes
- 出现位置:假期自习包/F-区间DP入门
英文原题面
Statement
The student council is preparing for the relay race at the sports festival. The council consists of members. They will run one after the other in the race, the speed of member is . The discrepancy of the -th stage is the difference between the maximum and the minimum running speed among the first members who ran. Formally, if denotes the speed of the -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 . 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?
Input
The first line contains a single integer () — the number of members of the student council. The second line contains integers () – the running speeds of the members.
Output
Print a single integer — the minimum possible value of after choosing the order of the members.
样例
样例 1
输入:
3
3 1 2
输出:
3
样例 2
输入:
1
5
输出:
0
样例 3
输入:
6
1 6 3 3 6 3
输出:
11
样例 4
输入:
6
104 943872923 6589 889921234 1000000000 69
输出:
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 , , and . We have:
- .
- .
- .
The resulting sum is . It can be shown that it is impossible to achieve a smaller value. In the second test case, the only possible rearrangement gives , so the minimum possible result is .
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |