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

Problem Arrangement

Back to problem

Editorial

An assignment is impossible if N<3N<3N<3 or either type is absent. Binary search a candidate width DDD, and sort each type separately. Let a type have minimum aaa and maximum bbb. If b−a≤Db-a\le Db−a≤D, leave the entire type in its outer division. Otherwise consider two choices. To leave bbb outside, every value below b−Db-Db−D is forced into Div.2. To leave aaa outside, every value above a+Da+Da+D is forced into Div.2. These choices are exhaustive. A feasible Div.2 cannot leave both extremes outside. If it contains aaa but not bbb, the outer division containing bbb forces the lower prefix. If it contains bbb but not aaa, it forces the upper suffix. If it contains both, its own width would exceed DDD, which is impossible. Try all four direction combinations for E and H. If the union of forced values has width at most DDD, place it in Div.2 and leave all other values in their respective outer divisions. Each forced set excludes the opposite extreme, so both outer divisions remain nonempty. If both forced sets are empty, move one item to Div.2 while leaving the outer divisions nonempty; this is possible because N≥3N\ge3N≥3 and both types exist. Subtask 1 enumerates the two legal division choices of every item in O(2NN)O(2^N N)O(2NN) time. The full solution sorts once and finds forced-set boundaries by binary search, taking O(Nlog⁡N+log⁡Vlog⁡N)O(N\log N+\log V\log N)O(NlogN+logVlogN) time and O(N)O(N)O(N) memory, where VVV is the difficulty range. Values and differences are at most 10910^9109, safely within signed 64-bit integers. Solution written by GPT6.1