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 ≤ 105). The next line contains those numbers, separated by a space. All numbers are integers and belong to the range from 1 to 109.
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个数,围成环,相邻的两个数相差1;成立yes,否则no;
思路:设得到得环为b数组,b[0]=最大值;如果map中有b[i-1]-1,取b[i-1]-1;否则取b[i-1]+1,没有就输出no;
最后特判b数组的最前面的数和最后面的数;(不要问我怎么证明,我不会,想了下可以就拍了一发,居然a了);
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#define true ture
#define false flase
using namespace std;
#define ll __int64
#define inf 0xfffffff
int scan()
{
int res = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
map<int,int>m;
int a[];
int b[];
int main()
{
int x,y,z,i,t=;
scanf("%d",&x);
for(i=;i<x;i++)
{
scanf("%d",&a[i]);
m[a[i]]++;
t=max(t,a[i]);
}
b[]=t;
m[t]--;
int ans=;
for(i=;i<x;i++)
{
if(m[b[i-]-])
{
b[i]=b[i-]-;
m[b[i-]-]--;
}
else if(m[b[i-]+])
{
b[i]=b[i-]+;
m[b[i-]+]--;
}
else
{
ans=;
break;
}
}
if(b[]-b[x-]!=)
ans=;
if(ans)
printf("NO\n");
else
printf("YES\n");
return ;
}