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 ≤
1
0
5
10^5
105). The next line contains those numbers, separated by a space. All numbers are integers and belong to the range from 1 to
1
0
9
10^9
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).
Examples
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
题目的规模有点大啊,暴搜肯定不行
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int a[100005];
int vis[100005];
int n,flag;
void dfs(int st,int dep){
//cout<<st<<" "<<dep<<endl;
if(flag){
return;
}
if(dep==n-1){
if(a[st]==a[0]+1||a[st]==a[0]-1){
flag=1;
}
return;
}
for(int i=st+1;i<n;i++){
if(a[i]==a[st]+1&&!vis[i]){
vis[i]=1;
dfs(i,dep+1);
vis[i]=0;
break;
}else if(a[i]-a[st]>=2){
break;
}
}
for(int i=st-1;i>=0;i--){
if(a[i]==a[st]-1&&!vis[i]){
vis[i]=1;
dfs(i,dep+1);
vis[i]=0;
break;
}else if(a[st]-a[i]>=2){
break;
}
}
}
int main() {
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
sort(a,a+n);
memset(vis,0,sizeof(vis));
vis[0]=1;
flag=0;
dfs(0,0);
if(flag){
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
return 0;
}
题解是俄语的根本看不懂,参考了别人的博客:https://www.cnblogs.com/qscqesze/p/4230857.html
无脑暴搜的时代已经一去不复返了
又是一道智商题,哭唧唧
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
#define inf 0x3f3f3f3f
int a[100005];
int t[100005];
int n,flag;
int main() {
ios::sync_with_stdio(false);
cin>>n;
int mi=inf;
for(int i=0; i<n; i++) {
cin>>a[i];
mi=min(mi,a[i]);
}
sort(a,a+n);
memset(t,0,sizeof(t));
for(int i=0; i<n; i++) {
if(a[i]-mi>=n) {
cout<<"NO"<<endl;
return 0;
}
t[a[i]-mi]++;
}
//212缩成2
//21212缩成2
//最终形成12
for(int i=1; i<a[n-1]-mi+1; i++) {
if(i!=a[n-1]-mi&&t[i]<=t[i-1]) {//缩点无法继续进行
cout<<"NO"<<endl;
return 0;
}
if(i==a[n-1]-mi&&t[i]!=t[i-1]) {//结果不为12这种情况
cout<<"NO"<<endl;
return 0;
}
t[i]-=t[i-1];
}
cout<<"YES"<<endl;
return 0;
}