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 Delivery

Back to problem

Editorial

Different counts of any type make delivery impossible. Equal counts are sufficient. Maintain the invariant that each stack is monochromatic and, when both are nonempty, their types differ. Let ccc be the next required type.

If the second stack contains ccc, execute 2. If the first stack contains ccc, execute 12. Otherwise, some unread input must have type ccc. Read input until reaching it, and deliver it immediately with 012.

Store each intervening type ddd as follows. If the second stack has type ddd, use 01. Otherwise, if the first stack is empty or has type ddd, use 0. In the remaining case the second stack must be empty, so use 01. If both stacks were occupied with types different from ddd, the three-type alphabet would force one to contain ccc, contradicting the current case. Check the second stack's matching type first to avoid splitting one type between both stacks. Each step preserves the invariant.

Every stick traverses input, first stack, second stack, and output exactly once, so there are exactly 3N3N3N commands. Time and space are O(N)O(N)O(N). Subtask 1 exhaustively searches and memoizes valid states; there are at most O(N33N)O(N^3 3^N)O(N33N states and time including string handling, sufficient for . For subtask 2, a single buffer stack suffices: push input until its top has the required type, then execute . With two types, the buffer is monochromatic after every delivery, giving time.

Solution written by GPT6.1

)
O(N43N)O(N^4 3^N)O(N43N)
N≤8N\leq8N≤8
12
O(N)O(N)O(N)