zoukankan      html  css  js  c++  java
  • 【31.95%】【CF 714B】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.

    【题解】

    这题看上去很吓人。

    但其实想想。如果有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;
    }


  • 相关阅读:
    Android开源项目收藏
    ubuntu修改ip获取方式(静态,动态)
    linux内核调用用户空间程序
    linux 改变目录下所有文件及其子文件夹下的权限
    jquery remove() empty()
    jquery之attr()和removeAttr() prop的使用场所
    jquery 获取对象的八种总结
    html子标签浮动父标签无法扩充
    静态代码块
    java static介绍
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7632247.html
Copyright © 2011-2022 走看看