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 = 0 , ch ; while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) ) { if( ch == EOF ) return 1 << 30 ; } res = ch - '0' ; while( ( ch = getchar() ) >= '0' && ch <= '9' ) res = res * 10 + ( ch - '0' ) ; return res ; } map<int,int>m; int a[100010]; int b[100010]; int main() { int x,y,z,i,t=0; scanf("%d",&x); for(i=0;i<x;i++) { scanf("%d",&a[i]); m[a[i]]++; t=max(t,a[i]); } b[0]=t; m[t]--; int ans=0; for(i=1;i<x;i++) { if(m[b[i-1]-1]) { b[i]=b[i-1]-1; m[b[i-1]-1]--; } else if(m[b[i-1]+1]) { b[i]=b[i-1]+1; m[b[i-1]+1]--; } else { ans=1; break; } } if(b[0]-b[x-1]!=1) ans=1; if(ans) printf("NO "); else printf("YES "); return 0; }