Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two array elements.
Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time.
The first line contains integer n (1 ≤ n ≤ 100) the number of elements in the array.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000) — the array elements.
In the single line print "YES" (without the quotes) if Yaroslav can obtain the array he needs, and "NO" (without the quotes) otherwise.
样例1: 1 1 样例2: 3 1 1 2 样例3: 4 7 7 7 7
样例1: YES 样例2: YES 样例3: NO
In the first sample the initial array fits well.
In the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it.
In the third sample Yarosav can't get the array he needs.
Problem Source: OYoung
题解:此题水题,主要是英语能不能看懂。(本宝宝一开始以为只能交换元素一次,其实可交换任意多次)由于元素能换成任意位置,所以其初始位置并无实际意义。于是我们只需关注元素的数目。不难发现,若某种元素的数目超过所有元素总数目的一半以上,便不能满足条件。因此我们可用一个book[n]数组来计算元素的数目,并筛选出数目最多的那种元素。再与所有元素总数目的一半进行比较即可。(注意对N的奇偶进行讨论)
本宝宝的码:
06.
#include<iostream>
07.
#include<iomanip>
08.
#include<cmath>
09.
using
namespace
std;
10.
11.
int
main()
12.
{
13.
int
a[101],book[1001]={0};
14.
int
N;
15.
cin >>N;
16.
int
max=0;
17.
for
(
int
i=0;i<N;i++){
18.
cin >>a[i];
19.
book[a[i]]++;
20.
if
(book[a[i]]>max){
21.
max=book[a[i]];
22.
}
23.
}
24.
if
(N==1){
25.
cout <<
"YES"
<<endl;
26.
}
27.
else
{
28.
if
(N%2 != 0){
29.
if
(max <= N/2+1){
30.
cout <<
"YES"
<<endl;
31.
}
32.
else
{
33.
cout <<
"NO"
<<endl;
34.
}
35.
}
36.
else
{
37.
if
(max <= N/2){
38.
cout <<
"YES"
<<endl;
39.
}
40.
else
{
41.
cout <<
"NO"
<<endl;
42.
}
43.
}
44.
}
45.
return
0;
46.
}