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.
枚举从0,1,2,3开始
贪心,先搞完0,然后1,然后2
例如0开始,到1,如果还有0回0,否则到2.。。。。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int q[100001],tot,n;
bool Try(int x,int a,int b,int c,int d) {
tot = 0;
if (x == 0) {
if (!a) return tot == n;
a--;q[++tot] = 0;
if(!b) return tot==n;
b--;q[++tot] = 1;
while(a) {
a--;q[++tot] = 0;
if (!b) return tot==n;
b--;q[++tot] = 1;
}
if(!c) return tot==n;
c--;q[++tot] = 2;
while(b) {
b--;q[++tot] = 1;
if(!c) return tot==n;
c--;q[++tot] = 2;
}
if (!d) return tot==n;
d--;q[++tot] = 3;
while(c) {
c--;q[++tot] = 2;
if(!d) return tot==n;
d--;q[++tot] = 3;
}
return tot==n;
}
if (x == 1) {
if (!b) return tot==n;
b--;q[++tot]=1;
while(a) {
a--;q[++tot] = 0;
if (!b) return tot==n;
b--;q[++tot] = 1;
}
if(!c) return tot==n;
c--;q[++tot] = 2;
while(b) {
b--;q[++tot] = 1;
if(!c) return tot==n;
c--;q[++tot] = 2;
}
if (!d) return tot==n;
d--;q[++tot] = 3;
while(c) {
c--;q[++tot] = 2;
if(!d) return tot==n;
d--;q[++tot] = 3;
}
return tot==n;
}
return 0;
}
int main() {
// int t;
// scanf("%d",&t);
// while(t--) {
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
n = a+b+c+d;
if(Try(0,a,b,c,d)) {
puts("YES");
for(int i = 1; i <= tot; i++)
printf("%d ",q[i]);
} else if (Try(1,a,b,c,d)) {
puts("YES");
for(int i = 1; i <= tot; i++)
printf("%d ",q[i]);
} else if(Try(0,d,c,b,a)) {
puts("YES");
for(int i = 1; i <= tot; i++)
printf("%d ",3-q[i]);
} else if (Try(1,d,c,b,a)) {
puts("YES");
for(int i = 1; i <= tot; i++)
printf("%d ",3-q[i]);
} else puts("NO");
// }
// }
return 0;
}