Yaroslav and Permutations

来源:互联网 发布:阿里云公共dns 编辑:程序博客网 时间:2024/04/28 20:20
Time Limit: 1sec    Memory Limit:256MB
Description

  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.

 

Input

 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.

Output

In the single line print "YES" (without the quotes) if Yaroslav can obtain the array he needs, and "NO" (without the quotes) otherwise.

Sample Input
 Copy sample input to clipboard
样例1:11样例2:31 1 2样例3:47 7 7 7
Sample Output
样例1:YES样例2:YES样例3:NO
Hint

In the first sample the initial array fits well.

In the second sample Yaroslav can get array: 121. 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.}

0 0