Codeforce 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.
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
YES
3
1 1 2
YES
4
7 7 7 7
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.
题解:统计出现次数最多的 如果大于一半 就不满足
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 #include <vector> 6 #include <cstdlib> 7 #include <iomanip> 8 #include <cmath> 9 #include <ctime> 10 #include <map> 11 #include <set> 12 using namespace std; 13 #define lowbit(x) (x&(-x)) 14 #define max(x,y) (x>y?x:y) 15 #define min(x,y) (x<y?x:y) 16 #define MAX 100000000000000000 17 #define MOD 1000000007 18 #define pi acos(-1.0) 19 #define ei exp(1) 20 #define PI 3.141592653589793238462 21 #define INF 0x3f3f3f3f3f 22 #define mem(a) (memset(a,0,sizeof(a))) 23 typedef long long ll; 24 ll gcd(ll a,ll b){ 25 return b?gcd(b,a%b):a; 26 } 27 const int N=105; 28 const int mod=1e9+7; 29 bool flag=0; 30 int a[N]; 31 int main() 32 { 33 std::ios::sync_with_stdio(false); 34 int n,t; 35 cin>>n; 36 for(int i=0;i<n;i++){ 37 cin>>t; 38 a[t]++; 39 if(a[t]>(n+1)/2) flag=1; 40 } 41 if(flag) cout<<"NO"<<endl; 42 else cout<<"YES"<<endl; 43 return 0; 44 }