Codeforces Round #604 (Div. 2) D. Beautiful Sequence(贪心)

Codeforces Round #604 (Div. 2) D. Beautiful Sequence(贪心)

传送门
Beautiful Sequence
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

Examples
input
2 2 2 1
output
YES
0 1 0 1 2 3 2
input
1 2 3 4
output
NO
input
2 2 2 3
output
NO
Note
In the first test, it is easy to see, that the sequence is beautiful because the difference between any two consecutive numbers is equal to 1. Also, there are exactly two numbers, equal to 0, 1, 2 and exactly one number, equal to 3.

It can be proved, that it is impossible to construct beautiful sequences in the second and third tests.
题意
给a,b,c,d四个数,对应0,1,2,3的个数,要求构造一个数列,使相邻的数之差为1。可以构造输出“YES”和数列,否则输出“NO”
解题思路
相对比较水的一道D题,裸眼可见的贪心。
首先考虑什么情况无法构造:

  1. 数列中0和1的个数有一个至少一个不为0,但3的个数大于2的个数。
    (a||b)&&(d>c)
  2. 数列中2和3的个数有一个至少一个不为0,但0的个数大于1的个数。
    (d||c)&&(a>b)
  3. 数列中0和1的个数为0,但2,3的个数差大于1。
    (c== 0&&d== 0&&abs(c-d)>1)
  4. 数列中2和3的个数为0,但0,1的个数差大于1。
    (a== 0&&b== 0&&abs(a-b)>1)
  5. 数列中0与1的个数差与数列中2与3的个数差相差大于1。
    (abs((b-a)-(c-d))>1)

处理完无法构造的,就可以贪心了,尽量多的以“01”或“23”这样填充,多余的1和2,再以“21”的形式填充,若还有剩余的1或2,分别填到开头或结尾。
Tips:
以字符串的形式储存数列,在处理填充的时候比较方便。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#define max(a,b)   (a>b?a:b)
#define min(a,b)   (a<b?a:b)
#define swap(a,b)  (a=a+b,b=a-b,a=a-b)
#define memset(x,y) memset(x,y,sizeof(x))
#define ll long long
using namespace std;
int a,b,c,d;
int main()
	{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	cin>>a>>b>>c>>d;
	if(((d||c)&&(a>b))||((a||b)&&(d>c)))
		{
		cout<<"NO";
		return 0;
		}
	if(a==0&&b==0)
		{
		if(abs(c-d)>1)
			{
			cout<<"NO";
			return 0;
			}
		else
			{
			string s="";
			cout<<"YES"<<endl;
			int j=1;
			for(int i=1; i<=min(c,d); i++)
				{
				s+="23";
				}
			if(c>d)s+="2";
			if(d>c)s="3"+s;
			for(int i=0; i<s.length(); i++)
				cout<<s[i]<<" ";
			return 0;
			}
		}
	if(c==0&&d==0)
		{
		if(abs(a-b)>1)
			{
			cout<<"NO";
			return 0;
			}
		else
			{
			string s="";
			cout<<"YES"<<endl;
			int j=1;
			for(int i=1; i<=min(a,b); i++)
				{
				s+="01";
				}
			if(a>b)s+="0";
			if(b>a)s="1"+s;
			for(int i=0; i<s.length(); i++)
				cout<<s[i]<<" ";
			return 0;
			}
		}
	if(a>b||d>c||abs((b-a)-(c-d))>1)
		{
		cout<<"NO";
		return 0;
		}
	else
		{
		int cha1=b-a,cha2=c-d;
		string s="";
		if(cha1-cha2==1)s+="1";
		while(a--)
			s+="01";
		for(int i=1; i<=min(cha1,cha2); i++)
			s+="21";
		while(d--)
			s+="23";
		if(cha2-cha1==1)s+="2";
		cout<<"YES"<<endl;
		for(int i=0; i<s.length(); i++)
			cout<<s[i]<<" ";
		return 0;
		}
	return 0;
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值