## Editorial for DMOPC '17 Contest 5 P6 - Bridges

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

There is no intended solution for the first subtask.

For the second subtask, specific information can be gained by asking an order, then swapping two consecutive elements of this order and asking that. In particular, consider the queries n-1 n-2 ... 4 3 2 1 and n-1 n-2 ... 4 3 1 2. Let the answers to these two queries by and respectively. Let bridge be the leftmost bridge which has not yet been built when bridge is built in the advisor's order. It can be shown that if , then must be , otherwise, (it is possible for to be , in which case, bridge is the last bridge to be built).

So now, we know that no bridge with index between and can be built after bridge and before bridge . We now repeat this solution for the bridges from to and the bridges from to . We also have to update the array and merge it appropriately. Clearly this solution requires less than queries. The time complexity is unimportant, it is at the very most .