Codeforces Round #552 (Div. 3) B. Make Them Equal

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a sequence a1,a2,…,ana1,a2,…,an consisting of nn integers.

You can choose any non-negative integer DD (i.e. D≥0D≥0), and for each aiai you can:

  • add DD (only once), i. e. perform ai:=ai+Dai:=ai+D, or
  • subtract DD (only once), i. e. perform ai:=ai−Dai:=ai−D, or
  • leave the value of aiai unchanged.

It is possible that after an operation the value aiai becomes negative.

Your goal is to choose such minimum non-negative integer DD and perform changes in such a way, that all aiai are equal (i.e. a1=a2=⋯=ana1=a2=⋯=an).

Print the required DD or, if it is impossible to choose such value DD, print -1.

For example, for array [2,8][2,8] the value D=3D=3 is minimum possible because you can obtain the array [5,5][5,5] if you will add DD to 22 and subtract DD from 88. And for array [1,4,7,7][1,4,7,7] the value D=3D=3 is also minimum possible. You can add it to 11 and subtract it from 77 and obtain the array [4,4,4,4][4,4,4,4].

Input

The first line of the input contains one integer nn (1≤n≤1001≤n≤100) — the number of elements in aa.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1001≤ai≤100) — the sequence aa.

Output

Print one integer — the minimum non-negative integer value DD such that if you add this value to some aiai, subtract this value from some aiai and leave some aiai without changes, all obtained values become equal.

If it is impossible to choose such value DD, print -1.

Examples

input

Copy

6
1 4 4 7 4 1

output

Copy

3

input

Copy

5
2 2 5 2 5

output

Copy

3

input

Copy

4
1 3 3 7

output

Copy

-1

input

Copy

2
2 8

output

Copy

3

因为数据量就100所以就一个一个枚举就行了

下面是我写的,不知道是什么原理竟然过了

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
set<int> s;
int a[100];
int main()
{
	int n;
	scanf("%d",&n);
	if (n==1)
	{
		printf("0\n");
		return 0;
	}
	for (int i=0;i<n;i++)
	{
		int x;
		scanf("%d",&x);
		s.insert(x);
	}
	int len = s.size();
	int k = 0;
	set<int>:: iterator it;
	for (it=s.begin();it!=s.end();it++)
	{
		a[k++] = *it;
	}
	/*for (int i=0;i<len;i++)
 	{
 		//if (a[i]==z) cnt++;
 		cout<<a[i]<<" ";
	}
	puts("");*/
	int c = a[len-1] - a[0];
	if (c%2==0) c /= 2;
	int z = a[len-1] - c;
	for (int i=0;i<len;i++)
	{
		if (a[i]<z) a[i] += c;
		else if (a[i]>z) a[i] -= c; 
 	} 
 	int cnt = 0;
 	for (int i=0;i<len;i++)
 	{
 		if (a[i]==z) cnt++;
	}
	if (cnt==len) printf("%d\n",c);
	else printf("-1\n");
	return 0;
} 

下面是暴力枚举做的 

#include<bits/stdc++.h>

using namespace std;

int a[110];

int main()
{
	int n;
	scanf("%d",&n);
	for (int i=0;i<n;i++)
		scanf("%d",&a[i]);
	sort(a,a+n);
	int flg = 0;
	for (int i=0;i<=100;i++)
	{
		int f = 1;
		int temp = a[0] + i;
		for (int j=1;j<n;j++)
		{
			if (a[j]==temp || a[j]+i==temp || a[j]-i==temp)
				continue;
			else
			{
				f = 0;
				break;
			}
		}
		if (f==1)
		{
			flg = 1;
			printf("%d\n",i);
			break;
		}
	}
	if (flg==0) printf("-1\n");
	return 0;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值