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-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

Red Cut

Back to problem

Editorial

Every cutting process corresponds to a full binary tree whose leaf order is fixed. An internal node's boundary is red exactly when the interval represented by that node has odd length.

Tracking the number of current odd-length pieces gives the possible number of red cuts:

  • If N=2mN=2mN=2m, then 0≤K≤m−10\leq K\leq m-10≤K≤m−1.
  • If N=2m+1N=2m+1N=2m+1, then 1≤K≤m1\leq K\leq m1≤K≤m.

For an odd interval of length 2m+12m+12m+1, call the following sets certificate blocks:

{1},{2,3},{4,5},⋯ ,{2m−2,2m−1},{2m}.\{1\},\{2,3\},\{4,5\},\cdots,\{2m-2,2m-1\},\{2m\}.{1},{2,3},{4,5},⋯,{2m−2,

Induction shows that every feasible red-boundary set contains a complete certificate block. Conversely, the following conditions are sufficient:

  • For length 2m2m2m, every boundary set of size at most m−1m-1m−1 is feasible.
  • For length 2m+12m+12m+1, every set of size between 111 and mm that contains a certificate block is feasible.

Sufficiency follows by strong induction, choosing the first cut so the remaining requirements split into smaller even and odd intervals.

If NNN is even, choose the KKK cheapest boundaries. If NNN is odd, enumerate a forced certificate block BBB and add the cheapest K−∣B∣K-|B|K−∣B∣ boundaries outside . After sorting costs, store prefix sums and each boundary's rank. Since a block has at most two elements, each candidate is evaluated in .

The total time complexity is O(Nlog⁡N)O(N\log N)O(NlogN) and the memory complexity is O(N)O(N)O(N).

Solution written by GPT5

2
m
−
1},{2m}.
m
BB
B
O(1)O(1)O(1)