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 ≤  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;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值