CodeForces - 296A-Yaroslav and Permutations(思维)
Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time.
Help Yaroslav.
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.
Examples
Input
1
1
Output
YES
Input
3
1 1 2
Output
YES
Input
4
7 7 7 7
Output
NO
Note
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.
题解:去看是否出现次数最多的是否占n个数的一半以上
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int a[105];
int vis[1005]={0};
memset(vis,0,sizeof(vis));
for(int t=0;t<n;t++)
{
scanf("%d",&a[t]);
vis[a[t]]++;
}
int maxn=0;
for(int t=1;t<=1000;t++)
{
if(vis[t]>=2)
{
if(vis[t]>maxn)
maxn=vis[t];
}
}
if(maxn<=(n+1)/2)
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
return 0;
}
最新文章
- EntityFramework linq 多条件查询,不定条件查询
- 最小生成树 prime poj1258
- .NET使用OpenSSL生成的pem密钥文件[1024位]
- spring mvc 用freemarker实现/user/edit?id=${id}=${type} 的替换
- 【Java每日一题】20161114
- HDU2955 Robberies[01背包]
- checkbox 全选,反选 ,全不选
- linux mono环境
- java生成base64编码的png
- 基于.Net FrameWork的 RestFul Service
- STM32学习笔记(二) 基于STM32-GPIO的流水灯实现
- CSS学习笔记——定位position属性的学习
- Unity Navigation面板了解
- 【转】AC算法详解
- 取消a标签在移动端点击时的背景颜色
- java--计时器
- CentOS7创建本地yum源
- hdu 2642
- Swift3.0:照片选择
- Overriding managed version XX for YY
热门文章
- IDEAL葵花宝典:java代码开发规范插件 (maven helper)解决maven 包冲突的问题
- test pic重复
- spark源码笔记
- hdu1085Holding Bin-Laden Captive!组合问题
- 【Codeforces】894E.Ralph and Mushrooms Tarjan缩点+DP
- 【译】在ES6中如何优雅的使用Arguments和Parameters
- hdu-2586 How far away ?(lca+bfs+dfs+线段树)
- [acm]HDOJ 2673 shǎ崽 OrOrOrOrz
- PCL推荐的命名规范(1)
- 在Elasticsearch6.X中如何实现去重