DMOPC '17 Contest 3 P2 - Towers of Hanoi

View as PDF

Submit solution


Points: 7 (partial)
Time limit: 5.0s
Memory limit: 64M

Author:
Problem type

The city of Hanoi has N towers, arranged in a line, numbered from 1 to N. Each one of the towers can be in one of two states: occupied or empty.

You have the ability to toggle the state of a tower of your choice (i.e. empty an occupied tower or occupy an empty tower). But when you do so, towers that are directly adjacent also toggle their state indirectly. Note that if you toggle tower 1, only towers 1 and 2 are toggled. The situation is similar for tower N: if you toggle tower N, only towers N and N-1 are toggled.

You are given the task of emptying all N towers. Can you accomplish this task in at most 10^6 steps?

It is guaranteed that this task is possible for the test cases given.

If there are multiple ways to do so, output any one of them.

Constraints

Subtask 1 [20%]

1 \leq N \leq 10

Subtask 2 [30%]

1 \leq N \leq 1000

Subtask 3 [50%]

1 \leq N \leq 10^5

Input Specification

The first line of input will contain an integer N.

The second line of input will contain N space-separated integers. The i^{\text{th}} integer will contain the state of tower numbered i. A 1 indicates the tower is occupied and a 0 indicates the tower is empty.

Output Specification

On the first line, print the total number of steps that you will take. If this number is larger than 10^6, the judge will not accept your output.

On the following lines, print the steps that you will take in order, one on each line. For each step, output the number of the tower that should be directly toggled on that step.

Sample Input 1

6
0 1 1 1 0 0

Sample Output 1

1
3

Sample Input 2

7
1 0 1 1 1 1 1

Sample Output 2

3
1
3
6

Comments


  • -2
    b1l4l  commented on Dec. 14, 2017, 4:43 p.m.

    In the problem set you said if you toggle 1 you toggle 1 & 2 that means you toggle n and n+1 not n and n-1 ? or am i wrong ?


    • 3
      aeternalis1  commented on Dec. 14, 2017, 5:19 p.m.

      The problem statement refers to the fact that toggling a tower also toggles those adjacent. Tower 1 is only adjacent to tower 2, since there is no tower 0, and tower N is only adjacent to tower N-1. This exception exists only for towers 1 and N, since all other towers have two others adjacent, one in front and one behind.