DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
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 account linking.

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

Completing a Permutation

Back to problem

Editorial

Treat positions as vertices and direct an edge from the position that must have the smaller value to the one that must have the larger value. Thus Si=S_i=Si​=< gives i→i+1i\to i+1i→i+1, and Si=S_i=Si​=> gives i+1→ii+1\to ii+1→i. This is an orientation of a path, so it has no directed cycle.

Let Li,UiL_i,U_iLi​,Ui​ be lower and upper bounds on the value assigned to position iii. Initialize an unknown position with Li=1,Ui=NL_i=1,U_i=N, and a fixed position with .

In topological order, apply Lv←max⁡(Lv,Lu+1)L_v\leftarrow\max(L_v,L_u+1)Lv​←max(Lv​,Lu​ for every edge . In reverse topological order, apply . If any , the instance is impossible. Otherwise, every edge satisfies and .

Now assign distinct values to positions, with position iii receiving a value in [Li,Ui][L_i,U_i][Li​,Ui​]. Process values x=1,2,…,Nx=1,2,\ldots,N. Add positions with to a priority queue, then assign to the position currently in the queue with the smallest . If the queue is empty or its smallest upper bound is below , no assignment exists.

Any assignment produced this way also satisfies the original inequalities. If all values are assigned, output the resulting permutation.

Topological sorting and bound propagation take O(N)O(N)O(N) time. Each position enters and leaves the queue once, giving total time O(Nlog⁡N)O(N\log N)O(NlogN) and space O(N)O(N)O(N).

Proof

Every valid permutation satisfies all propagated bounds. The initial intervals encode the original conditions. Since the values are integers, Pu<PvP_u<P_vPu​<Pv​ implies Pv≥Pu+1P_v\geq P_u+1. Thus propagating a lower bound cannot exclude a valid permutation, and the same holds for upper bounds.

Solution written by GPT6

Li​
=
1,Ui​=
N
Li=Ui=AiL_i=U_i=A_iLi​=Ui​=Ai​
+
1)
u→vu\to vu→v
Uu←min⁡(Uu,Uv−1)U_u\leftarrow\min(U_u,U_v-1)Uu​←min(Uu​,Uv​−1)
Li>UiL_i>U_iLi​>Ui​
Lu<LvL_u<L_vLu​<Lv​
Uu<UvU_u<U_vUu​<Uv​
x=
1,2,…,N
Li=xL_i=xLi​=x
xxx
UiU_iUi​
xxx
Pv​
≥
Pu​+
1

Consider the greedy interval assignment. Suppose some complete interval assignment agrees with the greedy choices for values smaller than xxx. Let iii be the position chosen by the greedy rule, and let jjj hold xxx in this complete assignment. Let y≥xy\geq xy≥x be the value at iii.

Swap the values at iii and jjj. Since iii is eligible, Li≤x≤y≤UiL_i\leq x\leq y\leq U_iLi​≤x≤y≤Ui​. Since jjj originally held xxx, we have Lj≤x≤yL_j\leq x\leq yLj​≤x≤y. The greedy choice gives Ui≤UjU_i\leq U_jUi​≤Uj​, so y≤Ui≤Ujy\leq U_i\leq U_jy≤Ui​≤Uj​. Both positions still satisfy their intervals. Therefore each greedy choice preserves the existence of a completion. An empty queue or an expired upper bound makes such a completion impossible.

Finally, consider an edge u→vu\to vu→v. Suppose vvv were assigned before uuu. At that moment, Lu<LvL_u<L_vLu​<Lv​ means that uuu has already entered the queue and is still unassigned. But Uu<UvU_u<U_vUu​<Uv​, so the greedy rule would prefer uuu to vvv, a contradiction. Hence Pu<PvP_u<P_vPu​<Pv​.

Each value is used exactly once, and every fixed position has a singleton interval containing its fixed value. Thus the output satisfies all conditions. Conversely, a valid permutation would survive bound propagation and interval assignment, so every impossibility report is correct.