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

block

Back to problem

Editorial

Suppose a block boundary is placed after position iii. After sorting and concatenating all blocks before that boundary, the result must equal the first iii elements of the fully sorted permutation. Therefore, P1,P2,⋯ ,PiP_1,P_2,\cdots,P_iP1​,P2​,⋯,Pi​ must consist of exactly 1,2,⋯ ,i1,2,\cdots,i1,2,⋯,i.

Because all elements of a permutation are distinct, the following conditions are equivalent.

  • P1,P2,⋯ ,PiP_1,P_2,\cdots,P_iP1​,P2​,⋯,Pi​ consists of exactly .

If the second condition holds, all first iii elements are at most iii. Since they are iii distinct values, they must be exactly the integers from 111 through iii.

Scan the permutation from left to right while maintaining the prefix maximum. Place a boundary at every position iii where the prefix maximum equals iii. Choosing all such boundaries is valid because the values between two consecutive boundaries form exactly the required consecutive value interval. Since every possible boundary is chosen, the number of blocks is maximum.

The time complexity is O(N)O(N)O(N), and the additional space complexity is O(1)O(1)O(1) when the input is processed as a stream.

Solution written by GPT5.6

1,2,⋯ ,i1,2,\cdots,i1,2,⋯,i
  • max⁡(P1,P2,⋯ ,Pi)=i\max(P_1,P_2,\cdots,P_i)=imax(P1​,P2​,⋯,Pi​)=i.