1772D.md (1825B)
1 Source: https://codeforces.com/problemset/problem/1772/D 2 3 D. Absolute Sorting 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 512 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 You are given an array $a$ consisting of $n$ integers. The array is sorted if $a_1 \le a_2 \le \dots \le a_n$. 22 23 You want to make the array $a$ sorted by applying the following operation exactly once: 24 25 * choose an integer $x$, then for every $i \in [1, n]$, replace $a_i$ by $|a_i - x|$. 26 27 Find any value of $x$ that will make the array sorted, or report that there is no such value. 28 29 Input 30 31 The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) — the number of test cases. 32 33 Each test case consists of two lines. The first line contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^8$). 34 35 Additional constraint on the input: the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. 36 37 Output 38 39 For each test case, print any integer $x$ ($0 \le x \le 10^9$) that makes the array sorted. It can be shown that if such an integer $x$ exists, there is at least one such integer between $0$ and $10^9$. 40 41 If there is no such integer, then print $-1$. If there are multiple suitable values of $x$, print any of them. 42 43 Example 44 45 Input 46 47 ``` 48 8 49 50 5 51 52 5 3 3 3 5 53 54 4 55 56 5 3 4 5 57 58 8 59 60 1 2 3 4 5 6 7 8 61 62 6 63 64 10 5 4 3 2 1 65 66 3 67 68 3 3 1 69 70 3 71 72 42 43 42 73 74 2 75 76 100000000 99999999 77 78 6 79 80 29613295 52036613 75100585 78027446 81409090 73215 81 ``` 82 83 Output 84 85 ``` 86 4 87 -1 88 0 89 42 90 2 91 -1 92 100000000 93 40741153 94 ``` 95 96 Note 97 98 In the first test case, after using $x = 4$, the array becomes $[1, 1, 1, 1, 1]$. 99 100 In the third test case, after using $x = 0$, the array becomes $[1, 2, 3, 4, 5, 6, 7, 8]$. 101 102 In the fourth test case, after using $x = 42$, the array becomes $[32, 37, 38, 39, 40, 41]$.