A16014 | Sasha and Array Coloring
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Sasha found an array $a$ consisting of $n$ 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)$ , where $S$ 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 = [\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 $1$ , $2$ and $5$ have color $1$ ; elements on positions $3$ and $4$ have color $2$ . Then:
- the cost of the color $1$ is $\max([1, 5, 4]) - \min([1, 5, 4]) = 5 - 1 = 4$ ;
- the cost of the color $2$ is $\max([6, 3]) - \min([6, 3]) = 6 - 3 = 3$ ;
- the total cost of the coloring is $7$ .
For the given array $a$ , you have to calculate the maximum possible cost of the coloring.
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)$ , where $S$ 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 = [\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 $1$ , $2$ and $5$ have color $1$ ; elements on positions $3$ and $4$ have color $2$ . Then:
- the cost of the color $1$ is $\max([1, 5, 4]) - \min([1, 5, 4]) = 5 - 1 = 4$ ;
- the cost of the color $2$ is $\max([6, 3]) - \min([6, 3]) = 6 - 3 = 3$ ;
- the total cost of the coloring is $7$ .
For the given array $a$ , you have to calculate the maximum possible cost of the coloring.
输入格式
The first line contains one integer $t$ ( $1 \leq t \leq 1000$ ) — the number of test cases.
The first line of each test case contains a single integer $n$ ( $1 \le n \le 50$ ) — length of $a$ .
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ( $1 \leq a_i \leq 50$ ) — array $a$ .
The first line of each test case contains a single integer $n$ ( $1 \le n \le 50$ ) — length of $a$ .
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ( $1 \leq a_i \leq 50$ ) — array $a$ .
输出格式
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 $[\color{red}{1}, \color{red}{5}, \color{blue}{6}, \color{blue}{3}, \color{red}{4}]$ . The answer is $(5 - 1) + (6 - 3) = 7$ .
In the second example, the only possible coloring is $[\color{blue}{5}]$ , for which the answer is $5 - 5 = 0$ .
In the third example, the optimal coloring is $[\color{blue}{1}, \color{red}{6}, \color{red}{3}, \color{blue}{9}]$ , the answer is $(9 - 1) + (6 - 3) = 11$ .
In the second example, the only possible coloring is $[\color{blue}{5}]$ , for which the answer is $5 - 5 = 0$ .
In the third example, the optimal coloring is $[\color{blue}{1}, \color{red}{6}, \color{red}{3}, \color{blue}{9}]$ , the answer is $(9 - 1) + (6 - 3) = 11$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted