Yaroslav and Permutations

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:
1
1

样例2:
3
1 1 2

样例3:
4
7 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值