DMOPC '17 Contest 4 P4 - Cops and Robbers

View as PDF

Submit solution


Points: 7 (partial)
Time limit: 1.0s
Memory limit: 256M

Author:
Problem type

In the beautiful capital of Dmojistan, there are N banks and a single policeman. The banks are numbered from 1 to N. You managed to find the policeman's schedule for the next N days. It turns out that on the ith day, he will be protecting bank ai.

Armed with this information, you are planning to rob all N banks in the next N days. You will rob bank bi on the ith day. A robbery will be successful if the cop is not protecting that bank on that day (that is, aibi).

Before you can start robbing, you need to determine a sequence b which will work. Output a sequence b which will rob all N banks or 1 if it is not possible to rob all N banks. The sequence should be N integers from 1 to N.

Any valid sequence will be accepted.

Constraints

1aiN

Subtask 1 [50%]

1N103

Subtask 2 [50%]

1N106

Input Specification

The first line will contain N.
The next line will contain N space-separated integers a1,a2,,aN.

Output Specification

Output a valid sequence b if it is possible or 1 if it is not. The sequence b should be N integers from 1 to N.

Sample Input

Copy
5
2 1 1 1 1

Sample Output

Copy
1 2 3 4 5

Comments


  • 2
    AlanCCCL2S18  commented on June 4, 2018, 3:25 p.m.

    I don't know what's getting me tle on the second subtask. Any ideas as to why this is happening?


    • 5
      wleung_bvg  commented on June 4, 2018, 4:52 p.m. edited

      Your algorithm appears to be O(N2) (due to ArrayDeque.contains()).


      • 2
        AlanCCCL2S18  commented on June 4, 2018, 6:58 p.m.

        Thanks for the feedback :)