A14696 | Closing The Gap
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
There are $n$ block towers in a row, where tower $i$ has a height of $a_i$ . You're part of a building crew, and you want to make the buildings look as nice as possible. In a single day, you can perform the following operation:
- Choose two indices $i$ and $j$ ( $1 \leq i, j \leq n$ ; $i \neq j$ ), and move a block from tower $i$ to tower $j$ . This essentially decreases $a_i$ by $1$ and increases $a_j$ by $1$ .
You think the ugliness of the buildings is the height difference between the tallest and shortest buildings. Formally, the ugliness is defined as $\max(a)-\min(a)$ .
What's the minimum possible ugliness you can achieve, after any number of days?
- Choose two indices $i$ and $j$ ( $1 \leq i, j \leq n$ ; $i \neq j$ ), and move a block from tower $i$ to tower $j$ . This essentially decreases $a_i$ by $1$ and increases $a_j$ by $1$ .
You think the ugliness of the buildings is the height difference between the tallest and shortest buildings. Formally, the ugliness is defined as $\max(a)-\min(a)$ .
What's the minimum possible ugliness you can achieve, after any number of days?
输入格式
The first line contains one integer $t$ ( $1 \leq t \leq 1000$ ) — the number of test cases. Then $t$ cases follow.
The first line of each test case contains one integer $n$ ( $2 \leq n \leq 100$ ) — the number of buildings.
The second line of each test case contains $n$ space separated integers $a_1, a_2, \ldots, a_n$ ( $1 \leq a_i \leq 10^7$ ) — the heights of the buildings.
The first line of each test case contains one integer $n$ ( $2 \leq n \leq 100$ ) — the number of buildings.
The second line of each test case contains $n$ space separated integers $a_1, a_2, \ldots, a_n$ ( $1 \leq a_i \leq 10^7$ ) — the heights of the buildings.
输出格式
For each test case, output a single integer — the minimum possible ugliness of the buildings.
输入输出样例
输入 #1
3 3 10 10 10 4 3 2 1 2 5 1 2 3 1 5
输出 #1
0 0 1
In the first test case, the ugliness is already $0$ .
In the second test case, you should do one operation, with $i = 1$ and $j = 3$ . The new heights will now be $[2, 2, 2, 2]$ , with an ugliness of $0$ .
In the third test case, you may do three operations:
1. with $i = 3$ and $j = 1$ . The new array will now be $[2, 2, 2, 1, 5]$ ,
2. with $i = 5$ and $j = 4$ . The new array will now be $[2, 2, 2, 2, 4]$ ,
3. with $i = 5$ and $j = 3$ . The new array will now be $[2, 2, 3, 2, 3]$ .
The resulting ugliness is $1$ . It can be proven that this is the minimum possible ugliness for this test.
In the second test case, you should do one operation, with $i = 1$ and $j = 3$ . The new heights will now be $[2, 2, 2, 2]$ , with an ugliness of $0$ .
In the third test case, you may do three operations:
1. with $i = 3$ and $j = 1$ . The new array will now be $[2, 2, 2, 1, 5]$ ,
2. with $i = 5$ and $j = 4$ . The new array will now be $[2, 2, 2, 2, 4]$ ,
3. with $i = 5$ and $j = 3$ . The new array will now be $[2, 2, 3, 2, 3]$ .
The resulting ugliness is $1$ . It can be proven that this is the minimum possible ugliness for this test.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted