#P5823. Interesting Class Schedule
Interesting Class Schedule
Interesting Class Schedule
You are given an integer n representing the number of subjects. In a school day there are exactly 2n classes. Each subject must appear exactly twice in the schedule. For each subject, define its gap as the number of classes between its two occurrences. The requirement is that the n gap values, when sorted in increasing order, form a consecutive sequence with a common difference of 1 (i.e. they are k, k+1, …, k+n-1 for some k, and in all known examples the natural choice is k = 1).
In other words, if for each subject i (with 1 ≤ i ≤ n) the two occurrences appear at positions a and b (with a < b), then the gap is defined as:
\(\text{gap}_i = b - a - 1\)
We require that the set \(\{\text{gap}_1, \text{gap}_2, \dots, \text{gap}_n\}\), when sorted in non-decreasing order, is exactly \(\{1, 2, \dots, n\}\.
This problem is equivalent to finding a Langford pairing (with a slight twist since for a Langford pairing the condition is that two occurrences of the number i have exactly i numbers between them). It is well known that such an arrangement exists if and only if n ≡ 0 or 3 (mod 4) (with the trivial case n = 1 considered separately). If it exists, you are to output one valid schedule; otherwise, output -1.
Note: The subjects are labeled with integers from 1 to n and the output schedule should contain 2n integers separated by spaces.
inputFormat
The input consists of a single integer n (1 ≤ n ≤ 20, for instance).
outputFormat
If a valid schedule exists, output a single line containing 2n integers (the subject numbers) separated by spaces. If no such schedule exists, output -1.
sample
1
-1