Codeforces Round #604 (Div. 2) D题

探讨了如何使用特定数量的整数0、1、2、3构建美丽序列,即任意两个连续数字之差为1的序列。文章详细分析了不同情况下序列构建的可能性,并提供了具体的算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Beautiful Sequence

An integer sequence is called beautiful if the difference between any two consecutive numbers is equal to 1. More formally, a sequence s1,s2,…,sn is beautiful if |si−si+1|=1 for all 1≤i≤n−1.

Trans has a numbers 0, b numbers 1, c numbers 2 and d numbers 3. He wants to construct a beautiful sequence using all of these a+b+c+d numbers.

However, it turns out to be a non-trivial task, and Trans was not able to do it. Could you please help Trans?

Input
The only input line contains four non-negative integers a, b, c and d (0<a+b+c+d≤105).

Output
If it is impossible to construct a beautiful sequence satisfying the above constraints, print “NO” (without quotes) in one line.

Otherwise, print “YES” (without quotes) in the first line. Then in the second line print a+b+c+d integers, separated by spaces — a beautiful sequence. There should be a numbers equal to 0, b numbers equal to 1, c numbers equal to 2 and d numbers equal to 3.

If there are multiple answers, you can print any of them.


模拟题,但是细节特别多,我们只要枚举每种数字数量的不同情况就行,但是这个情况比较多,非常容易写错;

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define lson k<<1
#define rson k<<1|1
#define inf 0x3f3f3f3f
//ios::sync_with_stdio(false);
using namespace std;
const int N=100100;
const int M=4001000;
const LL mod=998244353;
int main(){
	ios::sync_with_stdio(false);
	int a,b,c,d;
	cin>>a>>b>>c>>d;
	if(a==b&&c==d+1){
		cout<<"YES"<<endl;
		for(int i=1;i<=a;i++) cout<<0<<" "<<1<<" ";
		for(int i=1;i<=d;i++) cout<<2<<" "<<3<<" ";
		cout<<2<<endl;
	}
	else if(c==d&&b==a+1){
		cout<<"YES"<<endl;
		cout<<1<<" "; 
		for(int i=1;i<=a;i++) cout<<0<<" "<<1<<" ";
		for(int i=1;i<=c;i++) cout<<2<<" "<<3<<" ";
		cout<<endl;
	}
	else if(c>=d&&b>=a&&abs((c-d)-(b-a))<=1){
		cout<<"YES"<<endl;
		if(c-d==b-a){
			for(int i=1;i<=a;i++) cout<<0<<" "<<1<<" ";
			for(int i=1;i<=d;i++) cout<<2<<" "<<3<<" ";
			for(int i=1;i<=c-d;i++) cout<<2<<" "<<1<<" ";
			cout<<endl;
		}
		else if(c-d>b-a){
			for(int i=1;i<=a;i++) cout<<0<<" "<<1<<" ";
			for(int i=1;i<=d;i++) cout<<2<<" "<<3<<" ";	
			for(int i=1;i<=b-a;i++) cout<<2<<" "<<1<<" ";
			cout<<2<<endl;
		}
		else if(c-d<b-a){
			cout<<1<<" ";
			for(int i=1;i<=c-d;i++) cout<<2<<" "<<1<<" ";
			for(int i=1;i<=a;i++) cout<<0<<" "<<1<<" ";
			for(int i=1;i<=d;i++) cout<<2<<" "<<3<<" ";	
			cout<<endl;
		}
	}
	else if(a==0&&b==0&&d==c+1){
		cout<<"YES"<<endl;
		for(int i=1;i<=c;i++) cout<<3<<" "<<2<<" ";
		cout<<3<<endl; 
	}
	else if(c==0&&d==0&&a==b+1){
		cout<<"YES"<<endl;
		for(int i=1;i<=b;i++) cout<<0<<" "<<1<<" ";
		cout<<0<<endl;
	}
	else{
		cout<<"NO"<<endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值