Codeforces Round #839 (Div. 3)——D. Absolute Sorting

480 阅读3分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第24天,点击查看活动详情

Codeforces Round #839 (Div. 3)——D. Absolute Sorting

Problem - D - Codeforces

You are given an array aa consisting of nn integers. The array is sorted if a1≤a2≤⋯≤ana1≤a2≤⋯≤an.

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∈[1,n], replace aiai by |ai−x||ai−x|.

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

Input

The first line contains one integer tt (1≤t≤2⋅1041≤t≤2⋅104) — the number of test cases.

Each test case consists of two lines. The first line contains one integer nn (2≤n≤2⋅1052≤n≤2⋅105). The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1081≤ai≤108).

Additional constraint on the input: the sum of nn over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, print any integer xx (0≤x≤1090≤x≤109) 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 109109.

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

Example

input

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

output

4
-1
0
42
2
-1
100000000
40741153

问题解析

题目是说给你一个数组,你可以选一个数字x,把数组的每个元素ai变成|ai-x|,问能不能有一个数x可以把这个数组变成一个递增数组。

我们可以看一下,如果想把两个相邻的数变成递增的情况,我们可以选哪些数,很容易就可以推算出:

  • 如果ai小于等于ai+1,那么想维护他们的递增情况,我们可以选的数是:0 ~ (ai + a(i+1))/2;
  • 如果ai大于等于ai+1,那么想把他们变成递增情况,我们可以选的数是: (ai + a(i+1) + 1 )/2 ~ 1e9;

我们可以根据这样,得到n-1个区间,此时只要把这n-1个区间的范围收缩一下,如果范围内仍有数(左端点小于等于右端点),说明可以用x把数组变成递增的,输出区间内任意一个数即可;如果范围不正确,输出-1即可。

AC代码

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include <random>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<fstream>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>
#include<bitset>//#pragma GCC optimize(2)
//#pragma GCC optimize(3)#define endl '\n'
//#define int ll
#define PI acos(-1)
#define INF 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PII;
const int N = 2e5 + 50, MOD = 998244353;
​
int a[N];
bool check_up(int n)
{
    for (int i = 2; i <= n; i++)
        if (a[i] < a[i - 1])return false;
    return true;
}
bool check_down(int n)
{
    for (int i = 2; i <= n; i++)
        if (a[i] > a[i - 1])return false;
    return true;
}
void solve()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)cin >> a[i];
​
    vector<PII>v;
    for (int i = 1; i <= n - 1; i++)
    {
        if (a[i] == a[i + 1])continue;
        if (a[i] < a[i + 1])
        {
            v.push_back({ 0,(a[i] + a[i + 1]) / 2 });
        }
        else
        {
            v.push_back({ (a[i] + a[i + 1] + 1) / 2,1e9 });
        }
    }
    if (v.empty())
    {
        cout << 0 << endl;
        return;
    }
    PII ans = v[0];
    for (auto& i : v)
    {
        ans.first = max(ans.first, i.first);
        ans.second = min(ans.second, i.second);
    }
    if (ans.first > ans.second)
    {
        cout << -1 << endl;
    }
    else
        cout << ans.first << endl;
}
​
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}