DOJ logoDOJ
Problems
Recently added problemsSaved problems
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

Even Roasting

Back to problem

Editorial

If N=1N=1N=1, heat from the left once with power 111.

If N≥2N \geq 2N≥2, heat once from each side with power N−1N-1N−1. The iii-th piece from the left gains N−iN-iN−i from the first operation and i−1i-1i−1 from the second operation, so every piece finishes at a cooking level of N−1N-1N−1.

Thus, if N=1N=1N=1, print an operation count of 111 and the operation (1,1)(1,1)(1,1). Otherwise, print an operation count of 222 and the operations (1,N−1)(1,N-1) and . Both the time and space complexities for one test case are .

Proof

With no operations, every piece has a cooking level of 000. Since the final level must be positive, at least one operation is always necessary.

If N=1N=1N=1, a single operation with power 111 brings the only piece to a cooking level of 111. Therefore, the minimum number of operations is 111.

Now suppose . After just one operation, the first and second pieces from the chosen side have cooking levels and , respectively. Since , these levels differ. Therefore, one operation cannot make all cooking levels equal, and at least two operations are necessary.

In subtask 1, N=1N=1N=1 always holds, so print the single operation (1,1)(1,1)(1,1). Both the time and space complexities are O(1)O(1)O(1).

In subtask 2, exhaustive search is also possible. For each operation, try both directions and every power from 111 to . First check all candidates consisting of one operation. If none works, check all candidates consisting of two operations. Calculate the final cooking level of each piece one at a time and check that it equals the positive level of the first piece. The optimal construction above always uses powers at most and at most two operations, so this search range contains an optimal answer. The time complexity is , and the space complexity is .

(
1
,
N
−
1)
(2,N−1)(2,N-1)(2,N−1)
O(1)O(1)O(1)
N≥2N \geq 2
N≥2
PPP
max⁡(0,P−1)\max(0,P-1)max(0,P−1)
P≥1P \geq 1P≥1

Heating from the left with power N−1N-1N−1 increases the level of piece iii by max⁡(0,N−i)=N−i\max(0,N-i)=N-imax(0,N−i)=N−i. The same piece is the (N−i+1)(N-i+1)(N−i+1)-th piece from the right, so heating from the right with the same power increases its level by max⁡(0,i−1)=i−1\max(0,i-1)=i-1max(0,i−1)=i−1. After these two operations, its cooking level is (N−i)+(i−1)=N−1(N-i)+(i-1)=N-1(N−i)+(i−1)=N−1.

This level is independent of the position and is positive because N≥2N \geq 2N≥2. The heating power N−1N-1N−1 is also within the allowed range. The construction satisfies the conditions in two operations, and at least two are necessary, so it is optimal.

NN
N
NNN
O(N3)O(N^3)O(N3)
O(1)O(1)O(1)

For the full constraints, use the direct construction described above.

Solution written by GPT6