## Editorial for DMOPC '17 Contest 4 P4 - Cops and Robbers

Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: r3mark

For the first subtask, choose the unassigned bank guarded for the most unassigned days and rob it on an unassigned day when it is unguarded. Continue repeating this process until all banks have been robbed. This greedy algorithm will always find a solution as long as a single bank isn't guarded every day, in which case the program should output . It's important that the unassigned bank is based on the most unassigned days and not all days.

Time Complexity:

For the second subtask, there are two approaches. The first is to use a priority queue and set to implement the greedy solution in .

For the second approach, let be the set of banks which is guarded at least once. If , then a single bank is guarded every day and the program should output . Otherwise, list the elements of . For each element in , the first time the cop guards that bank, you should rob the bank before it in the list. Clearly, the bank before it in the list will be different from it. Now note that after doing this, you've robbed from all the banks which are ever guarded. So for the remaining banks, you can rob them arbitrarily.

For example, if the input is:

6
2 1 4 2 1 1

then is . Then the element before is , before is , and before is .

2 1 1 2 4 1
1 4 ? ? 2 ?

Then we fill the question marks with the remaining banks.

2 1 1 2 4 1
1 4 3 5 2 6

Time Complexity: