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


  • 相关阅读:
    css盒子模型、垂直外边距合并
    mov指令和 add以及sub 指令的区别
    第一章 基础知识
    字符串文档的去重
    python 之 字符串的常用方法
    python格式化输出之format用法
    python 格式化输出之%号
    c++11可变参数模板的使用1
    深入浅出 c++11 std::async
    std::thread 概述
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7632247.html
Copyright © 2020-2023  润新知