CF1658F.Juju and Binary String
省选/NOI-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
The cuteness of a binary string is the number of 1s divided by the length of the string. For example, the cuteness of 01101 is 53.
Juju has a binary string s of length n. She wants to choose some non-intersecting subsegments of s such that their concatenation has length m and it has the same cuteness as the string s.
More specifically, she wants to find two arrays l and r of equal length k such that 1≤l1≤r1<l2≤r2<…<lk≤rk≤n, and also:
- i=1∑k(ri−li+1)=m;
- The cuteness of s[l1,r1]+s[l2,r2]+…+s[lk,rk] is equal to the cuteness of s, where s[x,y] denotes the subsegment sxsx+1…sy, and + denotes string concatenation.
Juju does not like splitting the string into many parts, so she also wants to minimize the value of k. Find the minimum value of k such that there exist l and r that satisfy the constraints above or determine that it is impossible to find such l and r for any k.
一个二进制字符串的“可爱度”(cuteness)定义为其中字符 1 的个数除以字符串长度。例如,01101 的可爱度为 53。
Juju 有一个长度为 n 的二进制字符串 s。她希望从中选出若干个互不相交的子段,使得这些子段拼接后总长度恰好为 m,且拼接所得字符串的可爱度与原字符串 s 相同。
更准确地说,她希望找到两个长度均为 k 的数组 l 和 r,满足
1≤l1≤r1<l2≤r2<…<lk≤rk≤n,
且满足以下条件:
- i=1∑k(ri−li+1)=m;
- 字符串 s[l1,r1]+s[l2,r2]+…+s[lk,rk] 的可爱度等于 s 的可爱度,其中 s[x,y] 表示子段 sxsx+1…sy,而 + 表示字符串拼接。
Juju 不喜欢将字符串分割成太多段,因此她还希望最小化 k 的值。请找出满足上述约束的最小 k 值;若对任意 k 均不存在满足条件的 l 和 r,则判定为不可能。
输入格式
The first line contains a single integer t (1≤t≤104) — the number of test cases.
The first line of each test case contains two integers n and m (1≤m≤n≤2⋅105).
The second line of each test case contains a binary string s of length n.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
第一行包含一个整数 t(1≤t≤104)—— 测试用例的数量。
每个测试用例的第一行包含两个整数 n 和 m(1≤m≤n≤2⋅105)。
每个测试用例的第二行包含一个长度为 n 的二进制字符串 s。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, if there is no valid pair of l and r, print −1.
Otherwise, print k+1 lines.
In the first line, print a number k (1≤k≤m) — the minimum number of subsegments required.
Then print k lines, the i-th should contain li and ri (1≤li≤ri≤n) — the range of the i-th subsegment. Note that you should output the subsegments such that the inequality l1≤r1<l2≤r2<…<lk≤rk is true.
对于每个测试用例,若不存在满足条件的 l 和 r 对,则输出 −1。
否则,输出 k+1 行。
第一行输出一个整数 k(1≤k≤m),表示所需的最小子段数量。
随后输出 k 行,第 i 行应包含 li 和 ri(1≤li≤ri≤n),表示第 i 个子段的范围。注意:所输出的子段必须满足不等式 l1≤r1<l2≤r2<…<lk≤rk。
输入输出样例
输入#1
4 4 2 0011 8 6 11000011 4 3 0101 5 5 11111
输出#1
1 2 3 2 2 3 5 8 -1 1 1 5
说明/提示
In the first example, the cuteness of 0011 is the same as the cuteness of 01.
In the second example, the cuteness of 11000011 is 21 and there is no subsegment of size 6 with the same cuteness. So we must use 2 disjoint subsegments 10 and 0011.
In the third example, there are 8 ways to split the string such that i=1∑k(ri−li+1)=3 but none of them has the same cuteness as 0101.
In the last example, we don't have to split the string.
在第一个例子中,字符串 0011 的“可爱度”与字符串 01 的“可爱度”相同。
在第二个例子中,字符串 11000011 的“可爱度”为 21,且不存在长度为 6 的子段具有相同的“可爱度”。因此我们必须使用两个互不相交的子段 10 和 0011。
在第三个例子中,共有 8 种将字符串划分的方式使得 i=1∑k(ri−li+1)=3,但其中没有任何一种划分方式所得各子段的“可爱度”均与 0101 的“可爱度”相同。
在最后一个例子中,我们无需对字符串进行划分。
输入解题思路,AI测评打分。不知道怎么写?