CF1740C.Bricks and Bags

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

There are nn bricks numbered from 11 to nn. Brick ii has a weight of aia_i.

Pak Chanek has 33 bags numbered from 11 to 33 that are initially empty. For each brick, Pak Chanek must put it into one of the bags. After this, each bag must contain at least one brick.

After Pak Chanek distributes the bricks, Bu Dengklek will take exactly one brick from each bag. Let wjw_j be the weight of the brick Bu Dengklek takes from bag jj. The score is calculated as ∣w1−w2∣+∣w2−w3∣|w_1 - w_2| + |w_2 - w_3|, where ∣x∣|x| denotes the absolute value of xx.

It is known that Bu Dengklek will take the bricks in such a way that minimises the score. What is the maximum possible final score if Pak Chanek distributes the bricks optimally?

有 nn 块砖,编号从 11 到 nn。第 ii 块砖的重量为 aia_i。

Pak Chanek 有 33 个编号为 11 至 33 的包,初始时均为空。对于每一块砖,Pak Chanek 必须将其放入其中一个包中。分配结束后,每个包中至少包含一块砖。

在 Pak Chanek 分配完砖块后,Bu Dengklek 将从每个包中恰好取走一块砖。设 Bu Dengklek 从第 jj 个包中取走的砖的重量为 wjw_j。得分为 ∣w1−w2∣+∣w2−w3∣|w_1 - w_2| + |w_2 - w_3|,其中 ∣x∣|x| 表示 xx 的绝对值。

已知 Bu Dengklek 将以使得分最小化的方式取砖。若 Pak Chanek 以最优方式分配砖块,最终得分的最大可能值是多少?

输入格式

Each test contains multiple test cases. The first line contains an integer tt (1≤t≤2⋅1041 \leq t \leq 2 \cdot 10^4) — the number of test cases. The following lines contain the description of each test case.

The first line of each test case contains an integer nn (3≤n≤2⋅1053 \leq n \leq 2 \cdot 10^5) — the number of bricks.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1091 \leq a_i \leq 10^9) — the weights of the bricks.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤2⋅1041 \leq t \leq 2 \cdot 10^4),表示测试用例的数量。接下来的行描述各个测试用例。

每个测试用例的第一行包含一个整数 nn(3≤n≤2⋅1053 \leq n \leq 2 \cdot 10^5),表示砖块的数量。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1091 \leq a_i \leq 10^9),表示各砖块的重量。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output a line containing an integer representing the maximum possible final score if Pak Chanek distributes the bricks optimally.

对于每个测试用例,输出一行,包含一个整数,表示如果 Pak Chanek 最优地分配砖块,所能得到的最大最终得分。

输入输出样例

  • 输入#1

    3
    5
    3 1 5 2 3
    4
    17 8 19 45
    8
    265 265 265 265 265 265 265 265

    输出#1

    6
    63
    0

说明/提示

In the first test case, one way of achieving a final score of 66 is to do the following:

  • Put bricks 11, 44, and 55 into bag 11.
  • Put brick 33 into bag 22.
  • Put brick 22 into bag 33.

If Pak Chanek distributes the bricks that way, a way Bu Dengklek can take the bricks is:

  • Take brick 55 from bag 11.
  • Take brick 33 from bag 22.
  • Take brick 22 from bag 33.

The score is ∣a5−a3∣+∣a3−a2∣=∣3−5∣+∣5−1∣=6|a_5 - a_3| + |a_3 - a_2| = |3 - 5| + |5 - 1| = 6. It can be shown that Bu Dengklek cannot get a smaller score from this distribution.

It can be shown that there is no other distribution that results in a final score bigger than 66.

在第一个测试用例中,一种得到最终得分为 66 的方案如下:

  • 将砖块 11、44 和 55 放入袋子 11;
  • 将砖块 33 放入袋子 22;
  • 将砖块 22 放入袋子 33。

若 Pak Chanek 按照这种方式分配砖块,则 Bu Dengklek 可以按如下方式取砖:

  • 从袋子 11 中取走砖块 55;
  • 从袋子 22 中取走砖块 33;
  • 从袋子 33 中取走砖块 22。

此时得分为 ∣a5−a3∣+∣a3−a2∣=∣3−5∣+∣5−1∣=6|a_5 - a_3| + |a_3 - a_2| = |3 - 5| + |5 - 1| = 6。可以证明:对于该分配方式,Bu Dengklek 无法获得更小的得分。

可以证明:不存在其他分配方式能得到大于 66 的最终得分。

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

首页