2 seconds
256 megabytes
standard input
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?
The first line contains an integer n — how many numbers Anna had (3 ≤ n ≤ 10). The next line contains those numbers, separated by a space. All numbers are integers and belong to the range from 1 to 10.
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).
4
1 2 3 2
YES
6
1 1 2 2 2 3
YES
6
2 4 1 1 2 2
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[];
int mi=a[];
for(int i=;i<n;i++)
{
cin>>a[i];
mi=min(a[i],mi);
}
sort(a,a+n);
int flag=; for(int i=;i<n;i++)
{
if(a[i]-mi>=maxn)
{
flag=;
break;
}
t[a[i]-mi]++;
} if(flag==)
for(int i=;i<a[n-]-mi+;i++)
{
//cout<<t[i]<<endl;
if(t[i]==)
{
flag=;
break;
}
if(i!=a[n-]-mi&&t[i]<=t[i-])
{
flag=;
break;
}
if(i==a[n-]-mi&&t[i]!=t[i-])
{
flag=;
break;
}
t[i]-=t[i-];
}
if(flag==)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
return ;
}