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

B. Make Them Equal

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a sequence a1,a2,…,an consisting of n integers.

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

add D (only once), i. e. perform ai:=ai+D, or
subtract D (only once), i. e. perform ai:=ai−D, or
leave the value of ai unchanged.
It is possible that after an operation the value ai becomes negative.

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

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

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

Input

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

The second line of the input contains n integers a1,a2,…,an (1≤ai≤100) — the sequence a.

Output

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

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

Examples

input
6
1 4 4 7 4 1
output
3
input
5
2 2 5 2 5
output
3
input
4
1 3 3 7
output
-1
input
2
2 8
output
3

思路

找出一个数能使所有给出的数中的一些数通过加/减去这个数全部变成相等的数,给出的数中有一些重复的,需要去重,然后为了好看,可以进行排序,排序+去重=set

然后,如果集合中的数超过三个,那么问题肯定无解,比如1,2,3,4,不可能找到一个数使这四个数经过一些变换之后相等,所以超过三个数的集合直接输出-1;

如果集合中有三个数,那么肯定是第一个数和第三个数经过加上一个数和减去一个数变成中间的数,如果第二个数减去第一个数等于第三个数减去第二个数,那么这个差就是结果,如果不等,说明无解;

如果集合中有两个数,那么差就是第二个数减去第一个数,如果这个数是奇数,直接输出,如果是偶数,说明第一个数加上这个差的一半等于第二个数减去这个差的一半;

如果集合中有一个数,则结果为0.

Code

#include <iostream>
#include <set>
using namespace std;
int main()
{
    set<int> num;
    int n,temp=0,s[107],p[107];
    while (~scanf("%d",&n)){
        num.clear();
        for (int i = 0; i < n; ++i) {
            cin>>s[i];
            num.insert(s[i]);
        }
        set<int>::iterator it;
        for (it=num.begin();it!=num.end();it++)
            p[temp++]=*it;
        switch (temp)
        {
            case 1:
                cout<<'0'<<endl;
                break;
            case 2:
                if ((p[1]-p[0])%2!=0)
                {
                    cout<<p[1]-p[0]<<endl;
                    break;
                }
                else
                {
                    cout<<(p[1]-p[0])/2<<endl;
                    break;
                }
            case 3:
                if (p[1]-p[0]==p[2]-p[1])
                {
                    cout<<p[1]-p[0]<<endl;
                    break;
                }
                else
                {
                    cout<<"-1"<<endl;
                    break;
                }
            default:
                cout<<"-1"<<endl;
                break;
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/AlexKing007/p/12338412.html