Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help.
Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal.
Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal.
The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the number of integers in the Filya's array. The second line containsn integers a1, a2, ..., an (0 ≤ ai ≤ 109) — elements of the array.
If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes).
5 1 3 3 2 1
YES
5 1 2 3 4 5
NO
In the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements.
【题解】
这题看上去很吓人。
但其实想想。如果有4个或以上不同的数字。就根本不可能满足条件嘛。
3个的话是有可能的。就是中间那个数是中位数。
排序。然后去重,统计有多少个不同的数字。
【代码】
#include <cstdio> #include <algorithm> using namespace std; const int MAXN = 109000; int a[MAXN],n; void input_data() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); } void get_ans() { sort(a + 1, a + 1 + n); a[0] = 1; for (int i = 2;i <= n;i++) if (a[i] != a[i - 1])//这个去重方法是正确的 { a[0]++; a[a[0]] = a[i]; } } void output_ans() { if (a[0] <= 2) //一个数也可以! printf("YES "); else if (a[0] == 3 && (a[1] + a[3]) == (a[2] * 2)) printf("YES "); else printf("NO "); } int main() { //freopen("F:\rush.txt", "r", stdin); input_data(); get_ans(); output_ans(); return 0; }