This page is still under construction.

Parts of this page are still being built. What you see may change.

Cyclic Antimonotonic Permutations

Time limit2sMemory limit128 MB

Summary
For each n, output the lexicographically smallest permutation of 1 to n that is both antimonotonic (every middle element is a local min or max) and a single cycle when read as a pointer mapping.
Level

Hard9 of 10

Topics
Combinatorics, Math, Greedy, Implementation
Solved
No attempts yet

Problem

A permutation is a sequence of integers in which each integer from 11 to nn appears exactly once. In this problem we look for permutations p1,p2,…,pnp_1, p_2, \dots, p_n that satisfy both of the following properties:

  1. Antimonotonic: for every position ii with 1<i<n1 < i < n, the value pip_i must be either the smallest or the largest among the three neighbouring values pi−1,pi,pi+1p_{i-1}, p_i, p_{i+1}.
  2. Cyclic: treat each pip_i as a pointer leading from position ii to position pip_i. Starting at position 11 and following the pointers, you must be able to reach all nn positions before returning to position 11; that is, the permutation must consist of a single cycle of length nn.

Input

The input consists of several test cases. Each test case is a single line containing one integer nn (3≤n≤1063 \le n \le 10^6), the length of the permutation. The input is terminated by a line with n=0n = 0, which is not processed.

Output

For each test case, print on its own line the lexicographically smallest permutation of the integers 11 to nn that is both antimonotonic and cyclic. To compare two permutations aa and bb, compare the sequences a1,a2,…,ana_1, a_2, \dots, a_n and b1,b2,…,bnb_1, b_2, \dots, b_n from left to right; the permutation with the smaller value at the first position where they differ is the smaller one. Separate the integers on a line with single spaces.

Examples5

  1. Example 1

    Input
    3
    5
    10
    0
    
    Expected output
    2 3 1
    2 4 1 5 3
    2 4 1 6 3 8 5 10 7 9
    
  2. Example 2

    Input
    3
    0
    
    Expected output
    2 3 1
    
  3. Example 3

    Input
    4
    0
    
    Expected output
    2 4 1 3
    
  4. Example 4

    Input
    6
    0
    
    Expected output
    2 4 1 6 3 5
    
  5. Example 5

    Input
    7
    0
    
    Expected output
    2 4 1 6 3 7 5