#P7902. Construct a Sequence with Specific Gap Constraints

    ID: 21087 Type: Default 1000ms 256MiB

Construct a Sequence with Specific Gap Constraints

Construct a Sequence with Specific Gap Constraints

Given two positive integers \(n\) and \(d\), construct a sequence \(\{a_i\}_{i=1}^{2n}\) of length \(2n\) satisfying the following conditions:

  1. Each integer from \(1\) to \(n\) appears exactly twice.
  2. For each odd integer \(i\), if its two occurrences are at positions \(j\) and \(k\) with \(j d\).
  3. For each even integer \(i\), if its two occurrences are at positions \(j\) and \(k\) with \(j < k\), then \(k - j \le d\).

You may assume that a valid sequence exists for the given input. A constructive approach is as follows:

Idea: Partition the sequence into three contiguous parts. Let \(k = \lceil n/2 \rceil\) (which is the number of odd numbers between \(1\) and \(n\)). Place the odd numbers as follows: assign the first occurrence of each odd number in the left block (positions \(1\) to \(k\)) and the second occurrence in the right block (positions \(2n - k + 1\) to \(2n\)). This guarantees that for any odd \(i\) the gap is \(2n - k\) (which, by the input restriction, is greater than \(d\)). The remaining middle block (positions \(k+1\) to \(2n - k\)) is allocated for the even numbers. For even numbers, simply pair them in consecutive positions in the middle block; since consecutive positions differ by 1 (and \(d \ge 1\)), the condition \(\le d\) holds.

This construction satisfies all conditions provided that \(d < 2n - k\). It is guaranteed that the input will be such that a solution exists.

inputFormat

The input consists of two space-separated positive integers \(n\) and \(d\), where \(n\) is the number of distinct integers and \(d\) is the gap parameter.

outputFormat

Output the constructed sequence of \(2n\) integers separated by spaces.

sample

4 2
1 3 2 2 4 4 1 3