Editorial for LCC '24 Contest 2 J5 - Canoes


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: AndyMan68

Subtask 1

For this subtask, it is sufficient to simply simulate the K days of the canoes. This can be implemented by representing the docks as an array initially filled with -1, and for each canoer, keep searching by following the process until the dock array is set as -1 in that position. Once this is found, then replace it with the canoer number, and repeat. The only special canoer is canoer 1, which would have a predetermined place based on the days. Then, the current docking order array becomes the target order for the next day, and this can be continued for the next day. This is one such way to implement to implement this simulation process.

Time complexity: O(NK)

Subtask 2

For this subtask, pure simulation is too slow for larger values of K. To deal with such cases, 2 things can be observed:

Observation 1: After some time, the canoers positions become in a sorted state.

At some point, the order will be canoer 1, then canoer 2, and so on until dock N, and each canoer from dock 1 back to canoer 1 will be in increasing sorted order.

Such an order could be 6 7 8 1 2 3 4 5.

The proof of this is left as an exercise to the reader, although simply observing this through trial cases is sufficient.

Observation 2: After some time, the canoers positions begin to cycle.

More specifically, it will begin to cycle once it reaches this sorted state. Once this happens, each day after will begin to just shift each canoer by 1. Canoer 1 takes canoer 2's place, canoer 2 then takes canoer 3's place, and so on. In general, canoer M will take canoer (M+1)'s place.

By combining these two observations, we can conclude that once it becomes this sorted state, it will begin to cycle and remain sorted, and so determining the order for any large day number after this becomes trivial since canoer 1 will have a predetermined position.

There is a way to determine the exact day it becomes this sorted state without actually simulating and checking, however it is suffice to observe that it will reach this state some point before day N. However, if K happens to be before this sorted state occurs, then pure simulation is suffice as N is still relatively small for this subtask. Since for K < N is the slower case, the time complexity is simulating the O(N) process for at most N days.

Time complexity: O(N^2)

Subtask 3

For this subtask, the method of dealing with large K values (specifically K\ge N days) is the same, as it is still O(N). The main problem comes down to how to figure out the arrangmenet at the end of some day N\le K without simulating each of the K days. We can get around this by making a third observation:

Observation 3: All the canoers to the left of 1 will always be the numbered in order, ending with N to the left of 1.

For example, observe the order of 1 4 6 3 2 5 after some days:

End of day 1: 6 1 4 3 2 5

End of day 2: 5 6 1 3 2 4

End of day 3: 4 5 6 1 2 3

End of day 4: 3 4 5 6 1 2

The numbers to the left of 1 are always the numbers from N-K+1 to N.

This happens because after each day before the cycle period starts, the largest number to the right of 1 will move to position 1, displacing all numbers from dock 1 to the dock with canoer 1.

Since smaller canoers with smaller numbers will never be displaced or affected by canoers with larger numbers, all the canoers to the left of number 1 can be ignored when determining the arrangement of the canoers to the right of 1. Canoer 1 in a way "pushes" the canoers to the right until being pushed to the very front. Any canoer C can only be displaced or affected by canoers with a smaller number, and so when canoer 1 moves one to the right, all the canoers to the right of it will either stay in their original place or move to the right. With 1 less spot to the right of canoer 1 after they move to the right, the largest canoer to the right of canoer 1 ends up having to move to dock number 1.

We can then simulate by first placing down canoer 1 on their expected dock, and placing canoers N-K+1 to N to the left of canoer 1. Then going in order from canoer 2 to N-K+1 (all canoers from that number on already have been placed left of 1), we can simply place based on their initial day 1 position, and move them right if neccessary (if they ended up being displaced or pushed to the right). This way allows only 1 round of the simulation process for K\le N and no simulation process for K\ge N.

Time complexity O(N)


Comments

There are no comments at the moment.