Codeforces Round #371 (Div. 2)B. Filya and Homework

    xiaoxiao2022-06-28  33

    B. Filya and Homework time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

    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.

    Input

    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.

    Output

    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).

    Examples input 5 1 3 3 2 1 output YES input 5 1 2 3 4 5 output NO Note

    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.

    思路:看到身边的人普遍枚举有几个不同的数字的情况,如果这题刁难一点,那样做肯定炸。这其实还是一个思维题,直接找出最大的值跟最小的值,看他们的差值,如果差值是奇数,就肯定没有一个数到最大值最小值的距离相同。所以只可能有两个数,枚举所有数字是否都是最大值最小值,不是就不行。如果差值为偶数,枚举所有数是不是都是最大值最小值跟中间值; 我一开始就是这么想的,但是还是wa了2次。。。一开始以为是奇数就直接no。。看来我的思维缜密性还是不够。。。思维缜密性跟思维深度方面我还是要队友chenbin1好好学习、、、 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1e5 + 5; const int inf =2e9; int a[maxn]; int main() { int n; int max1 = - inf, min1 = inf; scanf("%d",&n); for(int i = 1; i <= n; i++) scanf("%d",&a[i]), min1 = min(min1,a[i]), max1 = max(max1,a[i]); if((max1 - min1) % 2) { int flag = 0; for(int i = 1; i <= n; i++) { if(a[i] != max1 && a[i] != min1) { flag = 1; printf("NO\n"); break; } } if(!flag) printf("YES\n"); } else { int flag = 0; long long m = max1 + min1; m /= 2; // cout <<max1 << ' ' << min1 << ' ' << m; for(int i = 1; i <= n; i++) { if(a[i] != m && a[i] != max1 && a[i] != min1) { printf("NO\n"); flag = 1; break; } } if(!flag) printf("YES\n"); } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-1124185.html

    最新回复(0)