题解
首先求出有多少不同的元素(tt),若tt>3显然是不可能通过加上或减去一个数使得所有数相等;若tt<3,则可以通过让较小的数加上差值就可以平衡;对于tt=3的情况,按照从小到大是顺序排序,若是等差数列,则可以达到平衡。
注意数据范围:
1
0
15
10^{15}
1015,一共16位,用
l
o
n
g
l
o
n
g
long\ long
long long。
代码
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<set>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define mem(a,s) memset(a,s,sizeof(a))
typedef long long ll;
const int N = 10000 + 10;
int n;
set<ll> s;
int main(){
// freopen("in.txt","r",stdin);
int TT;
cin>>TT;
while(TT--){
cin>>n;
int tt = 0;
if(s.size())
s.clear();
rep(i,1,n){
ll x;
cin >> x;
if(tt<3&&s.find(x)==s.end()){s.insert(x);tt++;}
}
if(tt>3)
cout << "NO\n";
if(tt<3)cout<<"YES\n";
if(tt==3){
ll a[3];
int tot = 0;
for(auto x:s)
{a[tot++] = x;}
if ((a[2] - a[1]) != (a[1] - a[0]))
cout << "NO\n";
else
cout << "YES\n";
}
}
return 0;
}