A15620 | Absolute Sorting
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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$ .
You want to make the array $a$ sorted by applying the following operation exactly once:
- choose an integer $x$ , then for every $i \in [1, n]$ , replace $a_i$ by $|a_i - x|$ .
Find any value of $x$ that will make the array sorted, or report that there is no such value.
You want to make the array $a$ sorted by applying the following operation exactly once:
- choose an integer $x$ , then for every $i \in [1, n]$ , replace $a_i$ by $|a_i - x|$ .
Find any value of $x$ that will make the array sorted, or report that there is no such value.
输入格式
The first line contains one integer $t$ ( $1 \le t \le 2 \cdot 10^4$ ) — the number of test cases.
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$ ).
Additional constraint on the input: the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ .
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$ ).
Additional constraint on the input: the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ .
输出格式
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$ .
If there is no such integer, then print $-1$ . If there are multiple suitable values of $x$ , print any of them.
If there is no such integer, then print $-1$ . If there are multiple suitable values of $x$ , print any of them.
输入输出样例
输入 #1
8 5 5 3 3 3 5 4 5 3 4 5 8 1 2 3 4 5 6 7 8 6 10 5 4 3 2 1 3 3 3 1 3 42 43 42 2 100000000 99999999 6 29613295 52036613 75100585 78027446 81409090 73215
输出 #1
4 -1 0 42 2 -1 100000000 40741153
In the first test case, after using $x = 4$ , the array becomes $[1, 1, 1, 1, 1]$ .
In the third test case, after using $x = 0$ , the array becomes $[1, 2, 3, 4, 5, 6, 7, 8]$ .
In the fourth test case, after using $x = 42$ , the array becomes $[32, 37, 38, 39, 40, 41]$ .
In the third test case, after using $x = 0$ , the array becomes $[1, 2, 3, 4, 5, 6, 7, 8]$ .
In the fourth test case, after using $x = 42$ , the array becomes $[32, 37, 38, 39, 40, 41]$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted