Yaroslav and Permutations
来源:互联网 发布:阿里云公共dns 编辑:程序博客网 时间:2024/04/28 20:20
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:11样例2:31 1 2样例3:47 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.
}
- A. Yaroslav and Permutations
- Yaroslav and Permutations
- Yaroslav and Permutations
- coderforce 296A Yaroslav and Permutations
- CodeForces 296A Yaroslav and Permutations
- CUIT ACM Personal Training 11.27(FM)D - Yaroslav and Permutations
- A. Yaroslav and Sequence
- D. Yaroslav and Divisors
- codeforces301A. Yaroslav and Sequence
- [CF301E]Yaroslav and Arrangements
- 【Codeforces301E】Yaroslav and Arrangements
- Permutations and Permutations II
- CodeForecs 301A Yaroslav and Sequence
- Codeforce 301D - Yaroslav and Divisors
- codeforces 301 A Yaroslav and Sequence
- Yaroslav and Points CF 295E
- Codeforces Yaroslav and Time(最短路)
- Yaroslav and Time (简单最短路)
- Android开发环境搭建(Eclipse for MAC)
- 面试常见问题及相关答案整理(持续更新中)
- 前端框架
- 102.Oracle数据库SQL开发之 表——获得有关表的信息
- c语言基础总结(一)
- Yaroslav and Permutations
- android studio AIDL使用方法。
- HDU1166(树状数组)
- Codeforces Round #333 (Div. 2) B C D
- 103.Oracle数据库SQL开发之 表——获得表中列的信息
- 5.1.5 time对象
- 交叉编译构建环境搭建
- UML——重要吗?
- 异常之java.net.BindException: Address already in use: JVM_Bind