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;
    }


  • 相关阅读:
    ORA01157、01110问题解决
    [转]WebService 中Maximum request length exceeded. 产生错误的原因,以及解决方法.
    [转]oracle9 startup时 报错ORA00600: 参数: [kcratr1_lostwrt]
    [转]八个dos命令
    [转]如何让ActiveX OCX控件下载并自动更新
    ora12154错误
    [转]Oracle分页
    [转]客户端连接数据库报错,ORA12516: TNS: 监听程序无法找到匹配协议栈的可用句柄
    vue中使用on事件被覆盖
    移动端滑动插件
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7632247.html
Copyright © 2011-2022 走看看