Editorial
Sort the problems of each contest by nondecreasing deadline. If two problems of the same contest appear in the opposite order, swapping them does not change the number of switches and cannot make the earlier deadline finish later. Therefore, the order inside each contest may be fixed, giving three chains.
Let contest contain problems, and let their preparation times and deadlines in this order be and . Tighten the deadlines backwards:
This does not change feasibility. After problem of one contest is completed, the later problems of that contest still require at least their total preparation time. Hence every schedule feasible for the original deadlines already satisfies all tightened deadlines. The converse is immediate because every tightened deadline is at most the original one.
After tightening,
Thus, whenever a state ending with a problem of one contest is feasible, continuing with the next problem of the same contest is always feasible.
Let . If problems of the three contests have been completed, the last problem belongs to contest , and the number of switches is , then the current time is
Define as the minimum number of switches for such a state, and define the analogous arrays for last contests and . The direct dynamic programming has states.
The tightened deadlines let us remove one dimension. For fixed , as increases, is impossible up to one position and has one constant value from the first feasible position onward. This follows by induction on the total number of completed problems: a same-contest transition propagates the current value to the right, while a transition from another contest creates one first feasible position and then receives the same propagation.
Store only the following pairs.
- : the minimum at which a state ending with contest first becomes feasible, together with its number of switches .
- and are defined symmetrically.
Suppose . Extend the current final block of contest until exactly problems of contest have been completed, then switch to contest . Completing the next problem of contest is valid exactly when
Because is increasing, the valid values of form either an empty set or one interval . Find by binary search. For every in this interval, apply
The other five transition types are symmetric.
Give a candidate the priority . The first component is the total number of completed problems represented by the first state of the candidate. Every transition increases this value by at least one, so the currently smallest candidate can never be improved by a future transition. Finalize that state and add its two outgoing interval updates.
Each table receives row intervals from one previous contest and column intervals from the other. The implementation stores each table twice, once in each orientation. Every copy must support interval minimum with a candidate pair, deletion of one finalized position, and retrieval of the globally smallest priority.
Encode a pair lexicographically as one integer. The interval operation becomes range chmin. A segment tree beats structure maintaining the maximum and second maximum handles each update in amortized . By additionally maintaining the best position among maximum-valued leaves and the best priority among the remaining leaves, the root directly gives the globally smallest candidate.
The number of finalized states is
Each state causes only a constant number of interval updates and point deletions. The total time complexity is and the memory complexity is .
Solution written by GPT5.6