Filya and Homework (CodeForces 714B) 水題
歡迎訪問https://blog.csdn.net/lxt_Lucia~~
宇宙第一小仙女\(^o^)/~~萌量爆表求帶飛=≡Σ((( つ^o^)つ~ dalao們點個關注唄~~
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 a
Now he wonders if it's possible to pick such integer x
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 contains n 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
Sample Output
YES
Sample Input
5 1 2 3 4 5
Sample Output
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.
題意:
給 n 個數,問是否可以通過給其中一些數加或減一次 x ,使得所有數相等,可以則輸出YES,不可以則輸出NO。
思路:
快排後通過離散化,求出這 n 個數的種類數。
1)若種類數 > 3,則肯定NO。
2)若種類數 < 3,則肯定YES。
3)若種類數 = 3,則要分情況,看最大的和最小的是否和中間的那個數差值絕對值相同,相同則YES,不相同則NO。
下面程式碼因為我記錄的是下標,陣列由 0 開始,所以 xb = 2 即種類數 = 3。
程式碼:
#include <cmath>
#include <stack>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define mem(a,b) memset(a,0,sizeof(a))
#define fori(a,b) for(int i=a;i<=b;i++)
#define forj(a,b) for(int j=a;j<=b;j++)
#define fork(a,b) for(int k=a;k<=b;k++)
#define ifor(a,b) for(int i=a;i>=b;i--)
#define jfor(a,b) for(int j=a;j>=b;j--)
using namespace std;
typedef long long LL;
int main()
{
int n,xb=0;
scanf("%d",&n);
LL a[n];
fori(0,n-1)
scanf("%lld",&a[i]);
sort(a,a+n);
int max1=a[n-1];
unique(a,a+n);
fori(0,n-1)
{
if(max1==a[i])
{
xb=i;
break;
}
}
if(xb>2)
printf("NO\n");
else if(xb<2)
printf("YES\n");
else
{
if(a[0]+a[2]==2*a[1])
printf("YES\n");
else
printf("NO\n");
}
return 0;
}