CF1772D.Absolute Sorting

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

You are given an array aa consisting of nn integers. The array is sorted if a1≤a2≤⋯≤ana_1 \le a_2 \le \dots \le a_n.

You want to make the array aa sorted by applying the following operation exactly once:

  • choose an integer xx, then for every i∈[1,n]i \in [1, n], replace aia_i by ∣ai−x∣|a_i - x|.

Find any value of xx that will make the array sorted, or report that there is no such value.

给你一个包含 nn 个整数的数组 aa。当满足 a1≤a2≤⋯≤ana_1 \le a_2 \le \dots \le a_n 时,称该数组是已排序的。

你希望通过恰好执行一次如下操作,使数组 aa 变为已排序的:

  • 选择一个整数 xx,然后对每个 i∈[1,n]i \in [1, n],将 aia_i 替换为 ∣ai−x∣|a_i - x|。

请找出任意一个能使数组变为已排序的 xx 值;若不存在这样的 xx,则报告无解。

输入格式

The first line contains one integer tt (1≤t≤2⋅1041 \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 nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5). The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤1081 \le a_i \le 10^8).

Additional constraint on the input: the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤2⋅1041 \le t \le 2 \cdot 10^4)—— 表示测试用例的数量。

每个测试用例由两行组成。第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5)。第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤1081 \le a_i \le 10^8)。

输入的额外约束:所有测试用例的 nn 值之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, print any integer xx (0≤x≤1090 \le x \le 10^9) that makes the array sorted. It can be shown that if such an integer xx exists, there is at least one such integer between 00 and 10910^9.

If there is no such integer, then print −1-1. If there are multiple suitable values of xx, print any of them.

对于每个测试用例,输出任意一个整数 xx(0≤x≤1090 \le x \le 10^9),使得数组变为有序。可以证明:若这样的整数 xx 存在,则至少存在一个满足条件的整数 xx 位于 00 到 10910^9 之间。

若不存在这样的整数,则输出 −1-1。若存在多个满足条件的 xx,输出其中任意一个即可。

输入输出样例

  • 输入#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=4x = 4, the array becomes [1,1,1,1,1][1, 1, 1, 1, 1].

In the third test case, after using x=0x = 0, the array becomes [1,2,3,4,5,6,7,8][1, 2, 3, 4, 5, 6, 7, 8].

In the fourth test case, after using x=42x = 42, the array becomes [32,37,38,39,40,41][32, 37, 38, 39, 40, 41].

在第一个测试用例中,使用 x=4x = 4 后,数组变为 [1,1,1,1,1][1, 1, 1, 1, 1]。

在第三个测试用例中,使用 x=0x = 0 后,数组变为 [1,2,3,4,5,6,7,8][1, 2, 3, 4, 5, 6, 7, 8]。

在第四个测试用例中,使用 x=42x = 42 后,数组变为 [32,37,38,39,40,41][32, 37, 38, 39, 40, 41]。

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

首页