Editorial for OTHS Coding Competition 3 (Mock CCC) S2 - Together Forever
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
This subtask is meant to reward brute-force solutions that run in , such as unoptimized versions of the full solution.
Subtask 2
The idea is to fix each index one by one to match Shinomiya's schedule. Since all courses in Shirogane's schedule are distinct, we can use a hash map to track the index of each course. Then, iterate from left to right. At each index , if
, continue. Otherwise, find the index
such that
using the hash map and swap
and
. After each swap, update the affected indices in the map.
This approach runs in time using constant-time hash lookups.
Subtask 3
To deal with duplicate courses, map each course to a set of indices where it occurs. Again, iterate from left to right. For each index , if
, do not swap anything. Otherwise, look for an occurrence of
in
that is after index
using the map, and swap with it. Then, update the affected indices in the sets. After processing each index
, immediately remove it from the set that contains it to ensure it is never swapped again.
Note that other data structures may be used instead of a set, such as a priority queue or monotonic stack.
This approach runs in either or
, depending on the data structure used.
Comments