题意介绍
给一个序列,问能不能使得序列中的某些数加上一个数K,某些数减去这个数K,余下的数不变,所有的数都相等。
题意分析
由分析可知,序列中最多有三个大小不等的数,否则肯定不行,因为set可以自动去重,所以用set存放序列,如果set中元素个数大于3,不可以,等于3,如果最大值减中间值等于中间值减最小值则可以,否则不可以。
通过代码
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
int t, n;
long long d, a[3];
int main() {
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n;
set<long long> s;
for (int i = 0; i < n; i++) {
cin >> d;
s.insert(d);
}
if (s.size() > 3) cout << "NO" << endl;
else if (s.size() == 3) {
set<long long>::iterator it = s.begin();
int count = 0;
while (it!=s.end()) {
a[count] = *it;
count++;
it++;
}
sort(a, a + 3);
if (a[1] - a[0] != a[2] - a[1]) cout << "NO" << endl;
else cout << "YES" << endl;
}
else cout << "YES" << endl;
}
return 0;
}