CF1509C.The Sports Festival

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The student council is preparing for the relay race at the sports festival.

The council consists of nn members. They will run one after the other in the race, the speed of member ii is sis_i. The discrepancy did_i of the ii-th stage is the difference between the maximum and the minimum running speed among the first ii members who ran. Formally, if aia_i denotes the speed of the ii-th member who participated in the race, then di=max⁡(a1,a2,…,ai)−min⁡(a1,a2,…,ai)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 d1+d2+⋯+dnd_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?

学生会正在为运动会的接力赛做准备。

学生会由 nn 名成员组成。他们将依次参加比赛,第 ii 名成员的速度为 sis_i。第 ii 个赛段的差异值 did_i 定义为前 ii 个参赛成员速度的最大值与最小值之差。形式化地,若 aia_i 表示第 ii 个参赛成员的速度,则

di=max⁡(a1,a2,…,ai)−min⁡(a1,a2,…,ai).d_i = \max(a_1, a_2, \dots, a_i) - \min(a_1, a_2, \dots, a_i).

你希望最小化所有差异值之和 d1+d2+⋯+dnd_1 + d_2 + \dots + d_n。为此,你可以任意调整成员的出场顺序。请问:所能达到的最小可能总和是多少?

输入格式

The first line contains a single integer nn (1≤n≤20001 \le n \le 2000) — the number of members of the student council.

The second line contains nn integers s1,s2,…,sns_1, s_2, \dots, s_n (1≤si≤1091 \le s_i \le 10^9) – the running speeds of the members.

第一行包含一个整数 nn(1≤n≤20001 \le n \le 2000)——学生会成员的数量。

第二行包含 nn 个整数 s1,s2,…,sns_1, s_2, \dots, s_n(1≤si≤1091 \le s_i \le 10^9)——各成员的跑步速度。

输出格式

Print a single integer — the minimum possible value of d1+d2+⋯+dnd_1 + d_2 + \dots + d_n after choosing the order of the members.

输出一个整数——在选定成员顺序后,d1+d2+⋯+dnd_1 + d_2 + \dots + d_n 的最小可能值。

输入输出样例

  • 输入#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 a1=2a_1 = 2, a2=3a_2 = 3, and a3=1a_3 = 1. We have:

  • d1=max⁡(2)−min⁡(2)=2−2=0d_1 = \max(2) - \min(2) = 2 - 2 = 0.
  • d2=max⁡(2,3)−min⁡(2,3)=3−2=1d_2 = \max(2, 3) - \min(2, 3) = 3 - 2 = 1.
  • d3=max⁡(2,3,1)−min⁡(2,3,1)=3−1=2d_3 = \max(2, 3, 1) - \min(2, 3, 1) = 3 - 1 = 2.

The resulting sum is d1+d2+d3=0+1+2=3d_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 d1=0d_1 = 0, so the minimum possible result is 00.

在第一个测试用例中,我们可以选择让第三位成员先跑,然后是第一位成员,最后是第二位成员。因此有 a1=2a_1 = 2,a2=3a_2 = 3,a3=1a_3 = 1。我们得到:

  • d1=max⁡(2)−min⁡(2)=2−2=0d_1 = \max(2) - \min(2) = 2 - 2 = 0。
  • d2=max⁡(2,3)−min⁡(2,3)=3−2=1d_2 = \max(2, 3) - \min(2, 3) = 3 - 2 = 1。
  • d3=max⁡(2,3,1)−min⁡(2,3,1)=3−1=2d_3 = \max(2, 3, 1) - \min(2, 3, 1) = 3 - 1 = 2。

最终的和为 d1+d2+d3=0+1+2=3d_1 + d_2 + d_3 = 0 + 1 + 2 = 3。可以证明,无法得到更小的值。

在第二个测试用例中,唯一可能的重排方式给出 d1=0d_1 = 0,因此可能的最小结果为 00。

输入解题思路,AI测评打分。不知道怎么写?

首页