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

Pepero Party

Back to problem

Editorial

Sort remaining givers and recipients by their numbers, and match equal ranks. Initially this is the identity matching. Maintain that this matching is valid and each matched recipient is at most the giver's original number.

For giver iii, let xxx be their matched recipient and yyy the previous remaining recipient, or 0 if none exists. Call iii blocked if Li>yL_i>yLi​>y. The first giver is always blocked. Choose the rightmost blocked giver iii.

The response jjj cannot precede xxx, since j≥Li>yj\geq L_i>yj≥Li​>y. Removing ii and shifts each intervening giver's match one recipient to the left. Every giver to the right of is unblocked, so its lower bound remains satisfied after the shift. Upper bounds remain satisfied because recipients only decrease. The matching stays valid, proving success on every turn against every interactor choice.

Maintain, for each remaining giver,

Di=rank⁡giver(i)−#{remaining recipients j<Li}−1,D_i=\operatorname{rank}_{\rm giver}(i)-\#\{\text{remaining recipients }j<L_i\}-1,Di​=rankgiver​(i)

using 1-based ranks. Validity implies Di≥0D_i\geq0Di​≥0, and blocked givers are exactly those with Di=0D_i=0Di​=0. After removing giver and recipient , decrement precisely for with . Indeed, any giver with lies to the right of , so its two rank changes cancel.

Divide original giver indices into blocks of size BBB. Within each block, sort by LiL_iLi​ and use a lazy segment tree for prefix decrements and the minimum. Materialize and rebuild the selected giver's block after directly updating it. For each complete block to its right, decrement the prefix with Lk≤jL_k\leq jLk​≤. Maintain a set of blocks whose minimum is zero to find the rightmost zero.

Time is O(N(B+(N/B)log⁡B))O(N(B+(N/B)\log B))O(N(B+(N/B)logB)) and space is O(N)O(N)O(N). The implementation uses B=1024B=1024. For , explicitly maintaining the sorted matching gives an solution. For , exhaustive minimax search over two masks of used givers and recipients is sufficient.

Solution written by GPT6.1

i
jjj
iii
−
#{remaining recipients j<
Li​}−
1,
ii
i
jjj
DkD_kDk​
k>ik>ik>i
Lk≤jL_k\leq jLk​≤j
Lk>jL_k>jLk​>j
iii
j
B
=
1024
N≤2000N\leq2000N≤2000
O(N2)O(N^2)O(N2)
N≤8N\leq8N≤8