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

Sorting a Sequence 5

Back to problem

Editorial

Let SSS be the initial sortedness and DDD the number of strict inversions. Swapping an adjacent inversion increases sortedness by one. If K≤DK\leq DK≤D, perform KKK insertion-sort swaps to attain S+KS+KS+K. Otherwise sort first. If duplicates exist, repeatedly swap an adjacent equal pair for the remaining operations. If all elements differ, spend pairs of operations swapping the same position twice; an odd remainder loses one unit of sortedness. Permutation parity makes this loss unavoidable.

Time is O(N2+K)O(N^2+K)O(N2+K) and sortedness is at most 499500499500499500. The small subtask can enumerate operation sequences with O(1)O(1)O(1) score updates per swap. The checker requires exactly KK valid indices and awards only for a correct optimum with a nonoptimal, properly formatted witness.

K
50%50\%50%