CF1843A.Sasha and Array Coloring

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Sasha found an array aa consisting of nn integers and asked you to paint elements.

You have to paint each element of the array. You can use as many colors as you want, but each element should be painted into exactly one color, and for each color, there should be at least one element of that color.

The cost of one color is the value of max⁡(S)−min⁡(S)\max(S) - \min(S), where SS is the sequence of elements of that color. The cost of the whole coloring is the sum of costs over all colors.

For example, suppose you have an array a=[1,5,6,3,4]a = [\color{red}{1}, \color{red}{5}, \color{blue}{6}, \color{blue}{3}, \color{red}{4}], and you painted its elements into two colors as follows: elements on positions 11, 22 and 55 have color 11; elements on positions 33 and 44 have color 22. Then:

  • the cost of the color 11 is max⁡([1,5,4])−min⁡([1,5,4])=5−1=4\max([1, 5, 4]) - \min([1, 5, 4]) = 5 - 1 = 4;
  • the cost of the color 22 is max⁡([6,3])−min⁡([6,3])=6−3=3\max([6, 3]) - \min([6, 3]) = 6 - 3 = 3;
  • the total cost of the coloring is 77.

For the given array aa, you have to calculate the maximum possible cost of the coloring.

萨莎找到了一个由 nn 个整数组成的数组 aa,并请你为其中的元素着色。

你需要为数组中的每个元素着色。你可以使用任意多种颜色,但每个元素必须且仅能被涂上一种颜色;并且每种颜色至少需用于一个元素。

一种颜色的代价定义为 max⁡(S)−min⁡(S)\max(S) - \min(S),其中 SS 是该颜色所对应的所有元素组成的序列。整个着色方案的总代价是所有颜色代价之和。

例如,假设你有一个数组 a=[1,5,6,3,4]a = [\color{red}{1}, \color{red}{5}, \color{blue}{6}, \color{blue}{3}, \color{red}{4}],并将元素按如下方式涂成两种颜色:位置 11、22 和 55 上的元素为颜色 11;位置 33 和 44 上的元素为颜色 22。那么:

  • 颜色 11 的代价为 max⁡([1,5,4])−min⁡([1,5,4])=5−1=4\max([1, 5, 4]) - \min([1, 5, 4]) = 5 - 1 = 4;
  • 颜色 22 的代价为 max⁡([6,3])−min⁡([6,3])=6−3=3\max([6, 3]) - \min([6, 3]) = 6 - 3 = 3;
  • 整个着色方案的总代价为 77。

对于给定的数组 aa,你需要计算着色方案所能达到的最大可能总代价。

输入格式

The first line contains one integer tt (1≤t≤10001 \leq t \leq 1000) — the number of test cases.

The first line of each test case contains a single integer nn (1≤n≤501 \le n \le 50) — length of aa.

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤501 \leq a_i \leq 50) — array aa.

第一行包含一个整数 tt(1≤t≤10001 \leq t \leq 1000)——测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤501 \le n \le 50)——数组 aa 的长度。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤501 \leq a_i \leq 50)——数组 aa。

输出格式

For each test case output the maximum possible cost of the coloring.

对于每个测试用例,输出着色方案的最大可能代价。

输入输出样例

  • 输入#1

    6
    5
    1 5 6 3 4
    1
    5
    4
    1 6 3 9
    6
    1 13 9 3 7 2
    4
    2 2 2 2
    5
    4 5 2 2 3

    输出#1

    7
    0
    11
    23
    0
    5

说明/提示

In the first example one of the optimal coloring is [1,5,6,3,4][\color{red}{1}, \color{red}{5}, \color{blue}{6}, \color{blue}{3}, \color{red}{4}]. The answer is (5−1)+(6−3)=7(5 - 1) + (6 - 3) = 7.

In the second example, the only possible coloring is [5][\color{blue}{5}], for which the answer is 5−5=05 - 5 = 0.

In the third example, the optimal coloring is [1,6,3,9][\color{blue}{1}, \color{red}{6}, \color{red}{3}, \color{blue}{9}], the answer is (9−1)+(6−3)=11(9 - 1) + (6 - 3) = 11.

在第一个例子中,一种最优染色方案为 [1,5,6,3,4][\color{red}{1}, \color{red}{5}, \color{blue}{6}, \color{blue}{3}, \color{red}{4}],答案为 (5−1)+(6−3)=7(5 - 1) + (6 - 3) = 7。

在第二个例子中,唯一可能的染色方案为 [5][\color{blue}{5}],答案为 5−5=05 - 5 = 0。

在第三个例子中,最优染色方案为 [1,6,3,9][\color{blue}{1}, \color{red}{6}, \color{red}{3}, \color{blue}{9}],答案为 (9−1)+(6−3)=11(9 - 1) + (6 - 3) = 11。

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

首页