D. Absolute Sorting

You are given an array a consisting of n integers. The array is sorted if a1≤a2≤⋯≤an.

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

choose an integer x, then for every i∈[1,n], replace ai by |ai−x|.
Find any value of x that will make the array sorted, or report that there is no such value.

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

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

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

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

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

Example
inputCopy
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
outputCopy
4
-1
0
42
2
-1
100000000
40741153
Note
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].

代码如下:

#include<bits/stdc++.h>
using namespace std;
#define ll long long

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	int n;
	cin>>n;
	int a[n];
	int Max=100000001;
	int Min=0;
	int k=0;
	for(int i=0;i<n;i++) cin>>a[i];
	for(int i=1;i<n;i++)
	{
		if(a[i]<a[i-1])
		{
			k=(a[i]+a[i-1]+1)/2;
		
			if(Min<k)
			{
				Min=k;
			}
		}
		if(a[i]>a[i-1])
		{	k=(a[i]+a[i-1])/2;
			
			if(Max>k)
			{
				Max=k;
			}
		}
	}
	if(Min>Max)
	{
		cout<<"-1"<<endl;
	}
	else
	{
		cout<<(Max+Min)/2<<endl;
	}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值