CF2187A.Restricted Sorting
普及-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given an array a of length n. For an integer k, we call it piggy if and only if it is possible to sort a in non-descending order by performing the following operation an arbitrary number of times (possibly zero):
- First, select two indices i and j (1≤i<j≤n) such that ∣ai−aj∣≥k;
- Then, swap ai and aj.
You need to determine the largest piggy integer k. If such an integer does not exist, output −1.
给你一个长度为 n 的数组 a。对于一个整数 k,当且仅当可以通过执行以下操作任意次(可以为零次)将 a 按非降序排列时,称其为“piggy”:
- 首先,选择两个下标 i 和 j(满足 1≤i<j≤n),使得 ∣ai−aj∣≥k;
- 然后,交换 ai 和 aj。
你需要找出最大的 piggy 整数 k。如果不存在这样的整数,则输出 −1。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤2⋅105) — the length of a.
The second line contains n integers a1,a2,…,an (1≤ai≤109) — the elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105)—— 数组 a 的长度。
第二行包含 n 个整数 a1,a2,…,an(1≤ai≤109)—— 数组 a 的元素。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, output a single integer — the largest piggy integer k.
If such an integer does not exist, output −1 instead.
对于每个测试用例,输出一个整数——最大的“存钱罐整数” k。
如果这样的整数不存在,则输出 −1。
输入输出样例
输入#1
5 1 1 5 1 2 3 4 5 3 1 4 2 5 2 1 5 4 3 6 1 1 4 5 1 4
输出#1
-1 -1 2 2 3
说明/提示
In the first and the second test case, no matter how large k is, you can always sort a in non-descending order by not performing any operations.
In the third test case, the largest piggy k is 2. We can select i=2 and j=3 in the first operation, since ∣a2−a3∣=∣4−2∣=2≥k, and a is sorted in non-descending order. It can be proven that no piggy integers larger than 2 exist.
在第一个和第二个测试用例中,无论 k 的值有多大,都不执行任何操作即可将数组 a 按非降序排列。
在第三个测试用例中,最大的“piggy”整数 k 为 2。我们可在第一次操作中选择 i=2 和 j=3,因为 ∣a2−a3∣=∣4−2∣=2≥k,从而使得 a 按非降序排列。可以证明:不存在大于 2 的 piggy 整数。
输入解题思路,AI测评打分。不知道怎么写?