良心正解求赞 — 题意题意
请你构造一个 0101 序列,序列需要满足以下所有要求:
恰好包含 nn 个 00 和 mm 个 11。
不存在两个或两个以上的 00 连续相邻。
不存在三个或三个以上的 11 连续相邻。
思路思路
先将nn个00排好
0 0 0 0 0 0
在任意两个00之间插入一个11
0 1 0 1 0 1 0 1 0 1 0
剩下的11可在开头,结尾或任意两个00之间添加
+ + 0 + 1 0 + 1 0 + 1 0 + 1 0 + 1 0 + +
只有满足
n − 1 ≤ m ≤ 2n + 2n − 1 ≤ m ≤ 2n + 2
时有解
良心正解
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
int main()
{
int n,m;
cin>>n>>m;
if(m<n-1||m>2*(n+1)) puts("-1");
else
{
for(int i=0;i<2;i++)
{
if(m>n-1)
{
printf("1");
m--;
}
}
while (n)
{
printf("0");
n--;
if(n)
{
printf("1");
m--;
if(m>n-1)
{
printf("1");
m--;
}
}
}
while(m--) printf("1");
}
return 0;
}
tql