Key Knocking
Time limit1sMemory limit512 MB
Given a binary string of length 3n, greedily pick operations in each 3-bit block to reach a weight of at least 2n within n moves.
- Level
Medium5 of 10
- Topics
- Greedy, Implementation, Array
- Solved
- No attempts yet
Problem
Goran is recovering from knee surgery and spends the time experimenting with a smart card that stores cryptographic keys. In this problem a key is a bitstring of length , where is a positive integer. The bits of the key are numbered from to , left to right. The weight of a key is the number of pairs of neighbouring bits that hold different values, plus . For example, the weight of the key "000" is , and the weight of the key "011010100" is .
Goran found out that he can tamper with the key by sending small jolts of electricity through the circuits of the smart card. To be precise, he can reliably perform the following operation as often as he wants. Pick any two neighbouring bits of the key and flip both of them. For example, one operation can change the key "000" into "110".
You are given a key of length . Find a way to turn it into a key of weight at least using at most operations. A solution always exists.
Input
The first line contains a string of the characters "0" and "1". This string is the initial key. The length of the key is , where is an integer with .
Output
On the first line print the number of operations . If , print on the second line the indices in the order the operations are performed, separated by single spaces. The -th number is the number of the left bit of the pair flipped in step . If , print only the first line.
Many operation sequences meet the weight requirement, so this problem accepts exactly the one sequence produced by the following rule.
Cut the key into blocks of three bits from the left. Block covers the positions , and . Process the blocks in order from to , and in each block read the current key, which earlier operations may already have changed.
In a block with , look at the three pairs , and . Try the following three actions in the written order and take the first one that leaves at least two of the three pairs holding different bits: do nothing, operate at index , operate at index .
In the last block, , look only at the pairs and . If at least one of them already holds different bits, do nothing, otherwise operate at index .
This rule never uses more than operations, and it always reaches weight at least .