zoukankan      html  css  js  c++  java
  • Codeforce 296A

    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.

     题解:统计出现次数最多的 如果大于一半 就不满足

     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 }
  • 相关阅读:
    php内存管理机制与垃圾回收机制
    PHP Laravel5实现的RBAC权限管理操作示例
    PHP实现微信企业付款到个人零钱步骤
    ThinkPHP 6.0 管道模式与中间件的实现分析
    深入讲解 Laravel 的 IoC 服务容器
    ThinkPHP6 核心分析:系统服务
    PHP 性能优化
    PHP 7.4 新语法:箭头函数
    深入理解 PHP 的 7 个预定义接口
    Java实现 LeetCode 795 区间子数组个数 (暴力分析)
  • 原文地址:https://www.cnblogs.com/wydxry/p/7264209.html
Copyright © 2011-2022 走看看