tinycodinglm / cf /cf-1003-B.jsonl
winapiadmin's picture
Upload folder using huggingface_hub
1f3a244 verified
|
Raw History Blame Contribute Delete
2.53 kB
opencode trace
System You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code.

You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code.

User

Problem: Binary String Constructing (Rating: 1300)
Tags: constructive algorithms

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 \le i < n$) such that $s_i \ne s_{i + 1}$. It is guaranteed that the answer always exists.

For example, for the string "01010" there are four indices $i$ such that $1 \le i < n$ and $s_i \ne s_{i + 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 \le a, b \le 100, 1 \le 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

Assistant

This problem has several general cases:
• $$$x$$$ is even and $$$a > b$$$, then the answer is 01 repeated $$$\frac{x}{2}$$$ times, then $$$b - \frac{x}{2}$$$ ones and $$$a - \frac{x}{2}$$$ zeroes;
• $$$x$$$ is even and $$$a \le b$$$, then the answer is 10 repeated $$$\frac{x}{2}$$$ times, then $$$a - \frac{x}{2}$$$ zeroes and $$$b - \frac{x}{2}$$$ ones;
• $$$x$$$ is odd and $$$a > b$$$, then the answer is 01 repeated $$$\lfloor\frac{x}{2}\rfloor$$$ times, then $$$a - \lfloor\frac{x}{2}\rfloor$$$ zeroes and $$$b - \lfloor\frac{x}{2}\rfloor$$$ ones;
• $$$x$$$ is odd and $$$a \le b$$$, then the answer is 10 repeated $$$\lfloor\frac{x}{2}\rfloor$$$ times, then $$$b - \lfloor\frac{x}{2}\rfloor$$$ ones and $$$a - \lfloor\frac{x}{2}\rfloor$$$ zeroes.
I am sure that there are other more beautiful solution, but for me the easiest way to solve this problem is to extract general cases and handle it.
Overall complexity is $$$O(a + b)$$$.