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

lis

Back to problem

Editorial

For subtask 111, let DiD_iDi​ be the length of a longest increasing subsequence ending at position iii. Check every j<ij<ij<i with Aj<AiA_j<A_iAj​<Ai​ and add 111 to the largest such DjD_jDj​. If no such jjj exists, set Di=1D_i=1Di​=1. Store the preceding index as well to reconstruct a sequence. This takes O(N2)O(N^2)O(N2) time.

For the full problem, maintain the smallest possible final value of an increasing subsequence for each length. When processing AiA_iAi​, find the first stored value at least AiA_iAi​ by binary search and update it. Equal values cannot extend a strictly increasing subsequence, so the search must find the first value greater than or equal to AiA_iA, not the first strictly greater value.

Along with each final value, store the index of the element that produced it. When AiA_iAi​ updates the state for length kkk, record the final index for length k−1k-1k−1 as its predecessor. Follow these predecessor indices from the final index of the longest length, then reverse the collected elements.

The time complexity is O(Nlog⁡N)O(N\log N)O(NlogN) and the space complexity is O(N)O(N)O(N) per case.

Solution written by GPT6

i​