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

V-Sort

Back to problem

Editorial

Treat each position as a vertex, and each allowed swap as an undirected edge. Edge swaps allow arbitrary rearrangement within a connected component. Sorting is therefore possible exactly when each component's current multiset equals its target multiset. Both positions Ai,BiA_i,B_iAi​,Bi​ have target value iii.

  • Subtask 1 (X=Y=0X=Y=0X=Y=0): only AiA_iAi​ and BiB_iB can be swapped. Both target values are , so every position must already be correct. Check this in time.

If X=NX=NX=N or Y=NY=NY=N, the corresponding edge type is absent. Check isolated vertices too. Since values may repeat, comparing sums alone is insufficient. Total time for the full solution is O(∑N)O(\sum N)O(∑N). All index and frequency magnitudes are at most 1 000 0001\,000\,000, so no arithmetic beyond signed 64-bit is required.

Solution written by GPT

i
​
iii
O(N)O(N)O(N)
  • Subtask 2 (X=Y=1X=Y=1X=Y=1): the two components are A1,B2,A3,B4,…A_1,B_2,A_3,B_4,\ldotsA1​,B2​,A3​,B4​,… and B1,A2,B3,A4,…B_1,A_2,B_3,A_4,\ldotsB1​,A2​,B3​,A. Each must contain every value from 111 to NNN exactly once. Frequency counting takes O(N)O(N)O(N) time and space. For N=1N=1N=1, these are two singleton components.
  • Subtask 3 (X=0X=0X=0): if Y=0Y=0Y=0, use subtask 1. Otherwise, AiA_iAi​ and BiB_iBi​ are connected and indices can differ by multiples of YYY. Components are residue classes of indices modulo YYY. Verify that each value vvv at index iii satisfies (v−1) mod Y=(i−1) mod Y(v-1)\bmod Y=(i-1)\bmod Y(v−1)modY=(i−1)modY, and that every value occurs exactly twice overall. This takes O(N)O(N)O(N) time and space.
  • Subtask 4 (N≤5N\leq5N≤5): the naive solution computes connectivity with Floyd–Warshall on 2N2N2N positions, then sorts and compares each component's current and target values. Time is O(N3)O(N^3)O(N3) and space is O(N2)O(N^2)O(N2).
  • Subtask 5 (N≤3 000N\leq3\,000N≤3000): build an adjacency matrix and find components by DFS, scanning every possible neighbor for each vertex. Compare frequency counts per component. Time and space are O(N2)O(N^2)O(N2). The per-file bound ∑N≤3 000\sum N\leq3\,000∑N≤3000 implies ∑N2≤9 000 000\sum N^2\leq9\,000\,000∑N2≤9000000.
  • Subtask 6: compute each vertex's at most two neighbors directly from its index and use iterative DFS without an adjacency matrix. Increment frequencies for current values and decrement them for target values. Check all touched entries and reset only those entries, rather than the entire array per component. Time and space are O(N)O(N)O(N).
  • 1000000
    4​
    ,
    …