【Codeforces】#604(div 2) D. Beautiful Sequence(构造,枚举起点)

题目链接:http://codeforces.com/contest/1265/problem/D

题意:

有a个0,b个1,c个2,d个3,构造一个序列,使每个相邻的数相差结果为1.

题解:

分别枚举以0,1,2,3开始的序列,寻找是否存在满足要求的序列。

#include<bits/stdc++.h>
using namespace std;
vector<int> ans;
int main()
{
	int a[5], b[5], id;
	cin >> a[1] >> a[2] >> a[3] >> a[4];
	for (int i = 1; i <= 4; i++)
	{
		for (int j = 1; j <= 4; j++)	b[j] = a[j];
		ans.clear();
		id = i;
		while (1)
		{
			if (id > 1 && b[id - 1] > 0)
			{
				id--;
				b[id]--;
				ans.push_back(id);
			}
			else if (id < 4 && b[id + 1] > 0)
			{
				id++;
				b[id]--;
				ans.push_back(id);
			}
			else break;
		}
		if (ans.size() == (a[1] + a[2] + a[3] + a[4]))
		{
			cout << "YES" << endl;
			for (int j = 0; j < ans.size(); j++)
				cout << ans[j] - 1 << ' ';
			return 0;
		}
	}
	cout << "NO" << endl;
	return 0;
}
发布了119 篇原创文章 · 获赞 20 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_40727946/article/details/103435136