1740C.md (2381B)
1 Source: https://codeforces.com/problemset/problem/1740/C 2 3 C. Bricks and Bags 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 There are $n$ bricks numbered from $1$ to $n$. Brick $i$ has a weight of $a_i$. 22 23 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. 24 25 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$. 26 27 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? 28 29 Input 30 31 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. 32 33 The first line of each test case contains an integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) — the number of bricks. 34 35 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. 36 37 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. 38 39 Output 40 41 For each test case, output a line containing an integer representing the maximum possible final score if Pak Chanek distributes the bricks optimally. 42 43 Example 44 45 Input 46 47 ``` 48 3 49 50 5 51 52 3 1 5 2 3 53 54 4 55 56 17 8 19 45 57 58 8 59 60 265 265 265 265 265 265 265 265 61 ``` 62 63 Output 64 65 ``` 66 6 67 63 68 0 69 ``` 70 71 Note 72 73 In the first test case, one way of achieving a final score of $6$ is to do the following: 74 75 * Put bricks $1$, $4$, and $5$ into bag $1$. 76 * Put brick $3$ into bag $2$. 77 * Put brick $2$ into bag $3$. 78 79 If Pak Chanek distributes the bricks that way, a way Bu Dengklek can take the bricks is: 80 81 * Take brick $5$ from bag $1$. 82 * Take brick $3$ from bag $2$. 83 * Take brick $2$ from bag $3$. 84 85 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. 86 87 It can be shown that there is no other distribution that results in a final score bigger than $6$.