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

Yet Another Permutation Problem 2

Back to problem

Editorial

Set Qi=N+1−PiQ_i=N+1-P_iQi​=N+1−Pi​ at every position. Because PPP contains each integer from 111 to NNN exactly once, QQQ also contains each of those integers exactly once. Thus, QQQ is a permutation.

For every iii, Pi+Qi=N+1P_i+Q_i=N+1Pi​+Qi​=N+. The sequence of sums is constant, so it is nondecreasing. This construction directly provides the guaranteed answer.

In subtask 1, Pi=iP_i=iPi​=i, so one may set Qi=N+1−iQ_i=N+1-iQi​=N+. In subtask 3, , so one may set . The general construction solves subtask 2 and the full subtask.

The time complexity for each subtask is O(T+∑N)O(T+\sum N)O(T+∑N). Processing input and output sequentially uses O(1)O(1)O(1) additional space.

Solution written by GPT5

1
1−
i
Pi=N+1−iP_i=N+1-iPi​=N+1−i
Qi=iQ_i=iQi​=i