CodeForces - 1003B Binary String Constructing
Posted 海岛Blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 1003B Binary String Constructing相关的知识,希望对你有一定的参考价值。
B. Binary String Constructing
time limit per test1 second
memory limit per test256 megabytes
You are given three integers a, b and x. Your task is to construct a binary string s of length n=a+b such that there are exactly a zeroes, exactly b ones and exactly x indices i (where 1≤i<n) such that si≠si+1. It is guaranteed that the answer always exists.
For example, for the string “01010” there are four indices i such that 1≤i<n and si≠si+1 (i=1,2,3,4). For the string “111001” there are two such indices i (i=3,5).
Recall that binary string is a non-empty sequence of characters where each character is either 0 or 1.
Input
The first line of the input contains three integers a, b and x (1≤a,b≤100,1≤x<a+b).
Output
Print only one string s, where s is any binary string satisfying conditions described above. It is guaranteed that the answer always exists.
Examples
input
2 2 1
output
1100
input
3 3 3
output
101100
input
5 3 6
output
01010100
Note
All possible answers for the first example:
1100;
0011.
All possible answers for the second example:
110100;
101100;
110010;
100110;
011001;
001101;
010011;
001011.
问题链接:CodeForces - 1003B Binary String Constructing
问题简述:(略)
问题分析:(略)
AC的C++语言程序(第二版,在第一版基础上修改得来)如下:
/* CodeForces - 1003B Binary String Constructing */
#include <bits/stdc++.h>
using namespace std;
int main()
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int a, b, x;
cin >> a >> b >> x;
if (a >= b)
cout << '0';
a--;
while (x > 2)
cout << "10";
a--, b--, x -= 2;
if (x & 1)
while (a--) cout << '0';
while (b--) cout << '1';
else
while (b--) cout << '1';
while (a--) cout << '0';
else
cout << '1';
b--;
while (x > 2)
cout << "01";
a--, b--, x -= 2;
if (x & 1)
while (b--) cout << '1';
while (a--) cout << '0';
else
while (a--) cout << '0';
while (b--) cout << '1';
return 0;
AC的C++语言程序(第一版)如下:
/* CodeForces - 1003B Binary String Constructing */
#include <bits/stdc++.h>
using namespace std;
int main()
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int a, b, x;
cin >> a >> b >> x;
if (x & 1)
if (a >= b)
cout << '0';
a--;
while (x > 2)
cout << "10";
a--, b--, x -= 2;
while (a--) cout << '0';
while (b--) cout << '1';
else
cout << '1';
b--;
while (x > 2)
cout << "01";
a--, b--, x -= 2;
while (b--) cout << '1';
while (a--) cout << '0';
else
if (a >= b)
cout << '0';
a--;
while (x > 2)
cout << "10";
a--, b--, x -= 2;
while (b--) cout << '1';
while (a--) cout << '0';
else
cout << '1';
b--;
while (x > 2)
cout << "01";
a--, b--, x -= 2;
while (a--) cout << '0';
while (b--) cout << '1';
return 0;
以上是关于CodeForces - 1003B Binary String Constructing的主要内容,如果未能解决你的问题,请参考以下文章
[CodeForces - 1225C]p-binary 数论二进制
Codeforces 1360H - Binary Median (二分)
Codeforces 1360H - Binary Median (二分)
CodeForces E. Binary Numbers AND Sum