A15405 | Bricks and Bags
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
There are $n$ bricks numbered from $1$ to $n$ . Brick $i$ has a weight of $a_i$ .
Pak Chanek has $3$ bags numbered from $1$ to $3$ 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 $w_j$ be the weight of the brick Bu Dengklek takes from bag $j$ . The score is calculated as $|w_1 - w_2| + |w_2 - w_3|$ , where $|x|$ denotes the absolute value of $x$ .
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?
Pak Chanek has $3$ bags numbered from $1$ to $3$ 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 $w_j$ be the weight of the brick Bu Dengklek takes from bag $j$ . The score is calculated as $|w_1 - w_2| + |w_2 - w_3|$ , where $|x|$ denotes the absolute value of $x$ .
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?
输入格式
Each test contains multiple test cases. The first line contains an integer $t$ ( $1 \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 $n$ ( $3 \leq n \leq 2 \cdot 10^5$ ) — the number of bricks.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $1 \leq a_i \leq 10^9$ ) — the weights of the bricks.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ .
The first line of each test case contains an integer $n$ ( $3 \leq n \leq 2 \cdot 10^5$ ) — the number of bricks.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $1 \leq a_i \leq 10^9$ ) — the weights of the bricks.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \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.
输入输出样例
输入 #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 $6$ is to do the following:
- Put bricks $1$ , $4$ , and $5$ into bag $1$ .
- Put brick $3$ into bag $2$ .
- Put brick $2$ into bag $3$ .
If Pak Chanek distributes the bricks that way, a way Bu Dengklek can take the bricks is:
- Take brick $5$ from bag $1$ .
- Take brick $3$ from bag $2$ .
- Take brick $2$ from bag $3$ .
The score is $|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 $6$ .
- Put bricks $1$ , $4$ , and $5$ into bag $1$ .
- Put brick $3$ into bag $2$ .
- Put brick $2$ into bag $3$ .
If Pak Chanek distributes the bricks that way, a way Bu Dengklek can take the bricks is:
- Take brick $5$ from bag $1$ .
- Take brick $3$ from bag $2$ .
- Take brick $2$ from bag $3$ .
The score is $|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 $6$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted