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

Permutation and LIS

Back to problem

Editorial

A strictly increasing subsequence of length NNN must be 1,2,…,N1,2,\ldots,N1,2,…,N. Let pvp_vpv​ be the position of vvv. Whenever pv+1<pvp_{v+1}<p_vpv+1​<pv​, a later copy is necessary. Greedily moving to the next copy exactly in these cases is sufficient. Thus the answer is one plus the number of such descents. Time and space are O(N)O(N)O(N); the answer is at most NNN. The naive solution for the small subtask appends copies and maintains LIS tails. At most NNN copies are needed, giving O(N2log⁡N)O(N^2\log N)O(N2logN) time and O(N)O(N)O(N) space.

Solution written by GPT6