Cyclic Antimonotonic Permutations
Time limit2sMemory limit128 MB
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 to appears exactly once. In this problem we look for permutations that satisfy both of the following properties:
- Antimonotonic: for every position with , the value must be either the smallest or the largest among the three neighbouring values .
- Cyclic: treat each as a pointer leading from position to position . Starting at position and following the pointers, you must be able to reach all positions before returning to position ; that is, the permutation must consist of a single cycle of length .
Input
The input consists of several test cases. Each test case is a single line containing one integer (), the length of the permutation. The input is terminated by a line with , which is not processed.
Output
For each test case, print on its own line the lexicographically smallest permutation of the integers to that is both antimonotonic and cyclic. To compare two permutations and , compare the sequences and 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.