CodeForces 128D Numbers 构造

D. Numbers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

One day Anna got the following task at school: to arrange several numbers in a circle so that any two neighboring numbers differs exactly by 1. Anna was given several numbers and arranged them in a circle to fulfill the task. Then she wanted to check if she had arranged the numbers correctly, but at this point her younger sister Maria came and shuffled all numbers. Anna got sick with anger but what's done is done and the results of her work had been destroyed. But please tell Anna: could she have hypothetically completed the task using all those given numbers?

Input

The first line contains an integer n — how many numbers Anna had (3 ≤ n ≤ 105). The next line contains those numbers, separated by a space. All numbers are integers and belong to the range from 1 to 109.

Output

Print the single line "YES" (without the quotes), if Anna could have completed the task correctly using all those numbers (using all of them is necessary). If Anna couldn't have fulfilled the task, no matter how hard she would try, print "NO" (without the quotes).

Sample test(s)
Input
4
1 2 3 2
Output
YES
Input
6
1 1 2 2 2 3
Output
YES
Input
6
2 4 1 1 2 2
Output
NO
题面意思:
大概就是讲有n张牌,这N张牌需要围成一个圈,要求牌和他相邻的牌最多相差1
问可不可行

思路:大概就是缩吧,比如 121可以看成1,12121也可以看成1。
在草稿本上画画就可以知道,最后状态是类似于12这种环
当然,出现断链的时候,当然是不可行的啦
于是就乱搞吧= =
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 100001
const int inf=0x7fffffff;   //无限大
int a[maxn];
int t[maxn];
int main()
{
    int n;
    while(cin>>n){
    cin>>a[0];
    int mi=a[0];
    for(int i=1;i<n;i++)
    {
        cin>>a[i];
        mi=min(a[i],mi);
    }
    sort(a,a+n);
    int flag=1;

    for(int i=0;i<n;i++)
    {
        if(a[i]-mi>=maxn)
        {
            flag=0;
            break;
        }
        t[a[i]-mi]++;
    }

    if(flag==1)
    for(int i=1;i<a[n-1]-mi+1;i++)
    {
        //cout<<t[i]<<endl;
        if(t[i]==0)
        {
            flag=0;
            break;
        }
        if(i!=a[n-1]-mi&&t[i]<=t[i-1])
        {
            flag=0;
            break;
        }
        if(i==a[n-1]-mi&&t[i]!=t[i-1])
        {
            flag=0;
            break;
        }
        t[i]-=t[i-1];
    }
    if(flag==0)
        cout<<"NO"<<endl;
    else
        cout<<"YES"<<endl;
    }
    return 0;
}
 

 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值