CF2187A.Restricted Sorting

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an array aa of length nn. For an integer kk, we call it piggy if and only if it is possible to sort aa in non-descending order by performing the following operation an arbitrary number of times (possibly zero):

  • First, select two indices ii and jj (1≤i<j≤n1 \le i \lt j \le n) such that ∣ai−aj∣≥k|a_i - a_j| \ge k;
  • Then, swap aia_i and aja_j.

You need to determine the largest piggy integer kk. If such an integer does not exist, output −1-1.

给你一个长度为 nn 的数组 aa。对于一个整数 kk,当且仅当可以通过执行以下操作任意次(可以为零次)将 aa 按非降序排列时,称其为“piggy”:

  • 首先,选择两个下标 ii 和 jj(满足 1≤i<j≤n1 \le i \lt j \le n),使得 ∣ai−aj∣≥k|a_i - a_j| \ge k;
  • 然后,交换 aia_i 和 aja_j。

你需要找出最大的 piggy 整数 kk。如果不存在这样的整数,则输出 −1-1。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the length of aa.

The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n (1≤ai≤1091 \le a_i \le 10^9) — the elements of aa.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 数组 aa 的长度。

第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(1≤ai≤1091 \le a_i \le 10^9)—— 数组 aa 的元素。

保证所有测试用例的 nn 之和不超过 2⋅1052\cdot10^5。

输出格式

For each test case, output a single integer — the largest piggy integer kk.

If such an integer does not exist, output −1-1 instead.

对于每个测试用例,输出一个整数——最大的“存钱罐整数” kk。

如果这样的整数不存在,则输出 −1-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 kk is, you can always sort aa in non-descending order by not performing any operations.

In the third test case, the largest piggy kk is 22. We can select i=2i=2 and j=3j=3 in the first operation, since ∣a2−a3∣=∣4−2∣=2≥k|a_2-a_3|=|4-2|=2 \ge k, and aa is sorted in non-descending order. It can be proven that no piggy integers larger than 22 exist.

在第一个和第二个测试用例中,无论 kk 的值有多大,都不执行任何操作即可将数组 aa 按非降序排列。

在第三个测试用例中,最大的“piggy”整数 kk 为 22。我们可在第一次操作中选择 i=2i=2 和 j=3j=3,因为 ∣a2−a3∣=∣4−2∣=2≥k|a_2-a_3|=|4-2|=2 \ge k,从而使得 aa 按非降序排列。可以证明:不存在大于 22 的 piggy 整数。

输入解题思路,AI测评打分。不知道怎么写?

首页