DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Setting Three Contests

Back to problem

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 xxx contain mxm_xmx​ problems, and let their preparation times and deadlines in this order be Ex,rE_{x,r}Ex,r​ and Dx,rD_{x,r}Dx,r​. Tighten the deadlines backwards:

Dx,r←min⁡(Dx,r,Dx,r+1−Ex,r+1)(1≤r<mx).D_{x,r}\leftarrow\min(D_{x,r},D_{x,r+1}-E_{x,r+1})\qquad(1\leq r<m_x).Dx,r​←min(D

This does not change feasibility. After problem rrr of one contest is completed, the later problems r+1,r+2,⋯ ,qr+1,r+2,\cdots,qr+1,r+2,⋯,q 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,

Dx,r+Ex,r+1≤Dx,r+1.D_{x,r}+E_{x,r+1}\leq D_{x,r+1}.Dx,r​+Ex,r+1​≤

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 Px[r]=Ex,1+Ex,2+⋯+Ex,rP_x[r]=E_{x,1}+E_{x,2}+\cdots+E_{x,r}Px​[r]=Ex,1​+. 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

P1[a]+P2[b]+P3[c]+Ks.P_1[a]+P_2[b]+P_3[c]+Ks.P1​[a]+P2​[b]+P

Define dp⁡1[a][b][c]\operatorname{dp}_1[a][b][c]dp1​[a][b][c] as the minimum number of switches for such a state, and define the analogous arrays for last contests 222 and 333. The direct dynamic programming has O( states.

The tightened deadlines let us remove one dimension. For fixed b,cb,cb,c, as aaa increases, dp⁡1[a][b][c]\operatorname{dp}_1[a][b][c]dp1​[a][b][c] 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.

  • F1[b][c]=(a,s)F_1[b][c]=(a,s)F1​[b][c]=(a,s): the minimum aaa at which a state ending with contest 11 first becomes feasible, together with its number of switches .

Suppose F1[b][c]=(a,s)F_1[b][c]=(a,s)F1​[b][c]=(a,s). Extend the current final block of contest 111 until exactly qq problems of contest have been completed, then switch to contest . Completing the next problem of contest is valid exactly when

P1[q]+P2[b+1]+P3[c]+K(s+1)≤D2,b+1.P_1[q]+P_2[b+1]+P_3[c]+K(s+1)\leq D_{2,b+1}.P1​[q]+P2​[

Because P1P_1P1​ is increasing, the valid values of qqq form either an empty set or one interval [a,r][a,r][a,r]. Find rrr by binary search. For every in this interval, apply

F2[q][c]←min⁡(F2[q][c],(b+1,s+1)).F_2[q][c]\leftarrow\min(F_2[q][c],(b+1,s+1)).F2​[q][c]←min(F2​

The other five transition types are symmetric.

Give a candidate (h,s)(h,s)(h,s) the priority (row+column+h,s)({\rm row}+{\rm column}+h,s)(row+column+h,s). 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 FxF_xFx​ 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 O(log⁡N)O(\log N)O(logN). 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

(m2+1)(m3+1)+(m1+1)(m3+1)+(m1+1)(m2+1)=O(N2).(m_2+1)(m_3+1)+(m_1+1)(m_3+1)+(m_1+1)(m_2+1)=O(N^2).(m2​+1)(m

Each state causes only a constant number of interval updates and point deletions. The total time complexity is O(N2log⁡N)O(N^2\log N)O(N2logN) and the memory complexity is O(N2)O(N^2)O(N2).

Solution written by GPT5.6

x,r
​
,
Dx,r+1​
−
Ex,r+1​)(1≤
r<
mx​).
Dx,r+1​.
Ex,2​
+
⋯+
Ex,r​
a,b,ca,b,ca,b,c
111
sss
3​
[
c
]
+
Ks.
m1m2m3)O(m_1m_2m_3)
O(m1​m2​m3​)
1
sss
  • F2[a][c]=(b,s)F_2[a][c]=(b,s)F2​[a][c]=(b,s) and F3[a][b]=(c,s)F_3[a][b]=(c,s)F3​[a][b]=(c,s) are defined symmetrically.
  • q
    111
    222
    222
    b
    +
    1]+
    P3​[c]+
    K(s+
    1)≤
    D2,b+1​.
    qq
    q
    [
    q
    ]
    [
    c
    ]
    ,
    (
    b
    +
    1,s+
    1)).
    3
    ​
    +
    1)+
    (m1​+
    1)(m3​+
    1)+
    (m1​+
    1)(m2​+
    1)=
    O(N2).