当前位置: 代码迷 >> 综合 >> Codeforces Problem 714B Filya and Homework(分类讨论)
  详细解决方案

Codeforces Problem 714B Filya and Homework(分类讨论)

热度:91   发布时间:2023-12-12 09:36:48.0

此文章可以使用目录功能哟↑(点击上方[+])

比赛链接→Codeforces Round #371 (Div. 2)

 Codeforces Problem 714B Filya and Homework

Accept: 0    Submit: 0
Time Limit: 1 second    Memory Limit : 256 megabytes

 Problem Description

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

 Sample Input

5
1 3 3 2 1
5
1 2 3 4 5

 Sample Output

YES
NO

 Hint

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.

 Problem Idea

解题思路:

【题意】

给你一个n个整数构成的数组a

问是否存在这样一个整数x,使得数组a中一些数加上x,一些数减去x,一些数维持不变,最终所有元素均相同

【类型】
分类讨论

【分析】

由"一些数加上x,一些数减去x,一些数维持不变"可知

数组a最多允许出现三种不同的数

一旦超过三种,无论怎么操作都是无法使最终所有数相同的

这时,考虑到std::set会自动过滤相同元素,所以我们可以使用std::set来简化编程

这样,我们就只需要判断最终std::set的大小size()即可

超过3,完全不需要考虑,输出"NO"

小于3的话肯定是"YES"(∵只有一种数时,x=0即可;若有两种数,我们只需将某一种加上x或另一种减去x就可以使俩数一致)

而有三种数时,需要考虑仨数是否构成等差数列,若能够构成等差数列,这样最小数加上公差,最大数减去公差就达到中间数了,从而所有数相同

【时间复杂度&&优化】
O(nlogn)

题目链接→Codeforces Problem 714B Filya and Homework

 Source Code

/*Sherlock and Watson and Adler*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 100005;
const int M = 20005;
const int inf = 1000000007;
const int mod = 1000000007;
set<int> s;
set<int>::iterator it;
int main()
{int n,i,a,ans=0;scanf("%d",&n);for(i=0;i<n;i++){scanf("%d",&a);s.insert(a);}if(s.size()>3)puts("NO");else if(s.size()<3)puts("YES");else{it=s.begin();ans+=*it;it++;it++;ans+=*it;it--;if(ans!=*it*2)puts("NO");elseputs("YES");}return 0;
}
菜鸟成长记

  相关解决方案