题目链接: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;
}