Codeforces Round #552 (Div. 3) B

 给你n个数,可以不变,可以加上D,可以减去D,能否变成同一个数,输出最小的D

#include 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=2e5+5;
int a[maxn],cnt;std::vector v;
signed main()
{
	ll n; cin >> n;
    vector a(n);
    ll mn = 1e9, mx = -1e9;
    set s;
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
        mn = min(mn, a[i]);
        mx = max(mx, a[i]);
        s.insert(a[i]);
    }
    if (s.size() > 3) {
        cout << -1;
        exit(0);
    }
    set::iterator q = s.begin();
    q++;
    set::iterator z = q;
    z++;
    if (s.size() == 2) {
        if ((*q - *s.begin()) % 2 == 0)
            cout << (*q - *s.begin()) / 2;
        else
            cout << (*q - *s.begin());
    } else if (s.size() == 3) {
        if (*q - *s.begin() == *z - *q)
            cout << *q - *s.begin();
        else
            cout << -1;
    } else {
        cout << 0;
    }
    cout<

 

你可能感兴趣的:(codeforces)