#P10854. Uniform Binary Sequence Transformation

    ID: 12897 Type: Default 1000ms 256MiB

Uniform Binary Sequence Transformation

Uniform Binary Sequence Transformation

You are given a binary sequence \(a_1,a_2,\ldots,a_n\) of length \(n\) (each element is either 0 or 1) and a positive integer \(r\) (either \(10^6\) or \(2\times10^6\)). You can perform an operation on the sequence as follows:

  • Select an index \(p\) such that either \(p=1\) or \(p=n\) or \(a_{p-1}\neq a_{p+1}\). (For indices with two neighbors, the condition is \(a_{p-1}\neq a_{p+1}\); the endpoints are always allowed.)
  • Flip \(a_p\) (i.e. change 0 to 1 or 1 to 0).

Your task is to make the sequence uniform (all 0's or all 1's) in at most \(r\) operations. You do not need to minimize the number of operations. If it is impossible to achieve a uniform sequence within \(r\) moves, output -1.


Note: It can be shown that when \(n \ge 3\) and the sequence is strictly alternating (i.e. no two consecutive elements are equal), it is impossible to make the sequence uniform. (The only exception is when \(n=2\), in which case both positions are endpoints and can be flipped arbitrarily.)

Input/Output Format: See below.

inputFormat

The first line contains two integers \(n\) and \(r\) (the length of the sequence and the maximum allowed number of operations).

The second line contains a binary string of length \(n\) (consisting of characters '0' and '1') representing \(a_1,a_2,\ldots,a_n\>.

outputFormat

If it is possible to make the sequence uniform in at most \(r\) operations, first output an integer \(k\) (the number of operations performed), followed by a line with \(k\) space‐separated integers indicating the indices (1-indexed) on which the operations are performed in order.

If it is impossible, output a single line containing -1.

sample

3 1000000
010
2

1 3

</p>