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

Permutations and Queries

Back to problem

Editorial

Let D=⌈N2⌉D=\left\lceil\cfrac{N}{2}\right\rceilD=⌈2N​⌉ and L=D−1L=D-1L=D−1. The minimum allowed interval length is LLL.

1. Swapping two distant positions

Suppose x<yx<yx<y and y−x≥Dy-x\geq Dy−x≥D. Reverse the following two intervals in order.

[x,y][x,y][x,y] [x+1,y−1][x+1,y-1][x+1,y−1]

The first reversal exchanges the two endpoints and reverses the interior. The second reversal restores only the interior. Therefore, only the elements at positions xxx and yyy are exchanged.

The second interval has length y−x−1≥D−1=Ly-x-1\geq D-1=Ly−x−1≥D−1, so both reversals are allowed. When and the two positions are adjacent, reversing only is sufficient.

=
L
N=2N=2N=2
[x,y][x,y][x,y]

2. A coordinate system with one fixed endpoint

First use position 111 as the endpoint. Let RRR be the reversal of [2,N][2,N][2,N]. It fixes position 111 and maps every other position xxx to N+2−xN+2-xN+2−x.

The two distances

x−1,N+1−xx-1,\qquad N+1-xx−1,N+1−x

sum to NNN. Therefore, at least one of them is at least DDD. If xxx is too close to position 111 in the current coordinate system, applying RRR once makes it sufficiently far away.

Treat the state after applying RRR as another coordinate system. Only the actual position representing each logical position changes. Swapping two actual positions in that coordinate system swaps the corresponding logical positions. One Boolean value is enough to remember whether this coordinate system is reversed.

The construction for endpoint NNN is symmetric and uses the reversal [1,N−1][1,N-1][1,N−1].

3. A cycle containing the endpoint

Consider a permutation cycle

(c1,c2,⋯ ,ck)(c_1,c_2,\cdots,c_k)(c1​,c2​,⋯,ck​)

where Pci=ci+1P_{c_i}=c_{i+1}Pci​​=ci+1​ and Pck=c1P_{c_k}=c_1Pck​​=c1​. Suppose c1c_1c1​ is the current endpoint.

Swap the following pairs of logical positions in order:

(c1,c2),(c1,c3),⋯ ,(c1,ck).(c_1,c_2),(c_1,c_3),\cdots,(c_1,c_k).(c1​,c2​),(c1​,c3​),⋯,(c1​,ck​).

These swaps fix every position in the cycle.

Before each swap, if the other position is too close to the endpoint, apply the auxiliary reversal RRR once. The two positions can then be exchanged with two reversals. The k−1k-1k−1 swaps require 2k−22k-22k−2 reversals. Coordinate-system changes are needed at most once before each swap and at most once more at the end, for at most kkk changes.

Thus, a cycle of length kkk containing the endpoint can be processed using at most

(2k−2)+k=3k−2(2k-2)+k=3k-2(2k−2)+k=3k−2

reversals.

4. A cycle not containing an endpoint

Choose an initial position c1c_1c1​ of the cycle.

If c1≥Lc_1\geq Lc1​≥L, reverse [1,c1][1,c_1][1,c1​]. This reversal is allowed and maps logical position c1c_1c1​ to actual position 111.

If c1<Lc_1<Lc1​<L, reverse [c1,N][c_1,N][c1​,N]. This reversal is also allowed and maps logical position c1c_1c1​ to actual position NNN.

Process the cycle in this coordinate system using the previous construction, and then reverse the initial interval again to restore the original coordinate system. The first and last reversals add two operations, so a cycle of length kkk needs at most

1+(3k−2)+1=3k1+(3k-2)+1=3k1+(3k−2)+1=3k

reversals.

5. Total number of operations and implementation

Cycles of length 111 require no operation. Process every other cycle independently. The sum of their lengths is at most NNN, so the total number of reversals is at most 3N3N3N.

In the implementation, compute the cycle decomposition of the permutation. For each cycle, maintain the selected endpoint, the initial coordinate transformation, and one Boolean value for the auxiliary reversal. There is no need to simulate the entire array.

The time complexity excluding the printed output is O(N)O(N)O(N), and the number of printed operations is O(N)O(N)O(N).

Solution written by GPT5.6