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

刺猬Filya收到了一个特别的家庭作业:通过增加或减少整数x来使一个整数数组的所有元素相等。本篇介绍了如何解决这个问题,并提供了一个具体的实现方案。
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;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值