CF2250B.String Construction
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given two integers n and k.
Construct a binary string∗ s of length n, such that both of the following conditions hold:
- The absolute difference between the number of characters 0 and the number of characters 1 in s is at most 1.
- There are exactly k pairs of adjacent equal characters in s. Formally, there are exactly k indices i (1≤i≤n−1) satisfying si=si+1.
Or determine that no such string exists.
∗A binary string is a string where each character is either 0 or 1.
给你两个整数 n 和 k。
构造一个长度为 n 的二进制字符串∗ s,使得以下两个条件均成立:
- 字符串 s 中字符 0 的个数与字符 1 的个数之差的绝对值至多为 1。
- s 中恰好有 k 对相邻且相等的字符。形式化地说,恰好存在 k 个下标 i(1≤i≤n−1),满足 si=si+1。
或者判断不存在满足条件的字符串。
∗二进制字符串是指每个字符均为 0 或 1 的字符串。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤1000). The description of the test cases follows.
The only line of each test case contains two integers n and k (2≤n≤2⋅105, 0≤k≤n−1).
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤1000)。随后是各测试用例的描述。
每个测试用例仅有一行,包含两个整数 n 和 k(2≤n≤2⋅105,0≤k≤n−1)。
保证所有测试用例的 n 值之和不超过 2⋅105。
输出格式
For each test case, output a binary string s of length n — the string you constructed. Print −1 if such a string does not exist.
If there are multiple answers, you may output any of them.
对于每个测试用例,输出一个长度为 n 的二进制字符串 s —— 即你构造出的字符串。如果不存在这样的字符串,则输出 −1。
如果有多个答案,你可以输出其中任意一个。
输入输出样例
输入#1
8 5 2 4 3 6 1 5 0 7 3 4 2 3 2 7 4
输出#1
01110 -1 101001 01010 0100011 0011 -1 0111000
说明/提示
In the first test case, one possible answer is s=01110. It contains three characters 1 and two characters 0, and there are exactly 2 adjacent equal pairs in s: (s2,s3) and (s3,s4).
In the second test case, k=n−1. All characters in s should be equal, so the numbers of characters 0 and 1 could not differ by at most 1. Thus, the answer is −1.
In the third test case, note that 010110 is also a possible answer.
在第一个测试用例中,一个可能的答案是 s=01110。它包含三个字符 1 和两个字符 0,且 s 中恰好有 2 对相邻相等的字符:(s2,s3) 和 (s3,s4)。
在第二个测试用例中,k=n−1。s 中所有字符必须相同,因此字符 0 和 1 的数量不可能至多相差 1。故答案为 −1。
在第三个测试用例中,请注意 010110 也是一个可能的答案。
输入解题思路,AI测评打分。不知道怎么写?