A14832 | Plus One on the Subset
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Polycarp got an array of integers $a[1 \dots n]$ as a gift. Now he wants to perform a certain number of operations (possibly zero) so that all elements of the array become the same (that is, to become $a_1=a_2=\dots=a_n$ ).
- In one operation, he can take some indices in the array and increase the elements of the array at those indices by $1$ .
For example, let $a=[4,2,1,6,2]$ . He can perform the following operation: select indices 1, 2, and 4 and increase elements of the array in those indices by $1$ . As a result, in one operation, he can get a new state of the array $a=[5,3,1,7,2]$ .
What is the minimum number of operations it can take so that all elements of the array become equal to each other (that is, to become $a_1=a_2=\dots=a_n$ )?
- In one operation, he can take some indices in the array and increase the elements of the array at those indices by $1$ .
For example, let $a=[4,2,1,6,2]$ . He can perform the following operation: select indices 1, 2, and 4 and increase elements of the array in those indices by $1$ . As a result, in one operation, he can get a new state of the array $a=[5,3,1,7,2]$ .
What is the minimum number of operations it can take so that all elements of the array become equal to each other (that is, to become $a_1=a_2=\dots=a_n$ )?
输入格式
The first line of the input contains a single integer $t$ ( $1 \le t \le 10^4$ ) — the number of test cases in the test.
The following are descriptions of the input test cases.
The first line of the description of each test case contains one integer $n$ ( $1 \le n \le 50$ ) — the array $a$ .
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ( $1 \le a_i \le 10^9$ ) — elements of the array $a$ .
The following are descriptions of the input test cases.
The first line of the description of each test case contains one integer $n$ ( $1 \le n \le 50$ ) — the array $a$ .
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ( $1 \le a_i \le 10^9$ ) — elements of the array $a$ .
输出格式
For each test case, print one integer — the minimum number of operations to make all elements of the array $a$ equal.
输入输出样例
输入 #1
3 6 3 4 2 4 1 2 3 1000 1002 998 2 12 11
输出 #1
3 4 1
First test case:
- $a=[3,4,2,4,1,2]$ take $a_3, a_5$ and perform an operation plus one on them, as a result we get $a=[3,4,3,4,2,2]$ .
- $a=[3,4,3,4,2,2]$ we take $a_1, a_5, a_6$ and perform an operation on them plus one, as a result we get $a=[4,4,3,4,3,3]$ .
- $a=[4,4,3,4,3,3]$ we take $a_3, a_5, a_6$ and perform an operation on them plus one, as a result we get $a=[4,4,4,4,4,4]$ .
There are other sequences of $3$ operations, after the application of which all elements become equal.
Second test case:
- $a=[1000,1002,998]$ 2 times we take $a_1, a_3$ and perform an operation plus one on them, as a result we get $a=[1002,1002,1000]$ .
- $a=[1002,1002,1000]$ also take $a_3$ 2 times and perform an operation plus one on it, as a result we get $a=[1002,1002,1002]$ .
Third test case:
- $a=[12,11]$ take $a_2$ and perform an operation plus one on it, as a result we get $a=[12,12]$ .
- $a=[3,4,2,4,1,2]$ take $a_3, a_5$ and perform an operation plus one on them, as a result we get $a=[3,4,3,4,2,2]$ .
- $a=[3,4,3,4,2,2]$ we take $a_1, a_5, a_6$ and perform an operation on them plus one, as a result we get $a=[4,4,3,4,3,3]$ .
- $a=[4,4,3,4,3,3]$ we take $a_3, a_5, a_6$ and perform an operation on them plus one, as a result we get $a=[4,4,4,4,4,4]$ .
There are other sequences of $3$ operations, after the application of which all elements become equal.
Second test case:
- $a=[1000,1002,998]$ 2 times we take $a_1, a_3$ and perform an operation plus one on them, as a result we get $a=[1002,1002,1000]$ .
- $a=[1002,1002,1000]$ also take $a_3$ 2 times and perform an operation plus one on it, as a result we get $a=[1002,1002,1002]$ .
Third test case:
- $a=[12,11]$ take $a_2$ and perform an operation plus one on it, as a result we get $a=[12,12]$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted