Octopus
Time limit1sMemory limit1024 MB
Given N octopuses in a circle, each adjacent pair holds a numbered hand (1 to 8), find the lexicographically smallest length-N sequence of hand numbers that is realizable.
- Level
Medium6 of 10
- Topics
- Greedy, Math, Combinatorics, Implementation
- Solved
- No attempts yet
Problem
It is well known that an octopus has eight arms. But you have probably never heard that octopuses call their arms 1, 2, 3, ..., 8! There is no rule that the numbers go in clockwise order or anything like that. Of course, such an octopus may exist. For convenience, this problem calls arms hands instead.
The octopuses are going to dance ganggangsullae for the first full moon of the year. Each octopus joins hands with the two different octopuses on either side to form a circle. There is etiquette to follow when octopuses hold hands.
- They must hold hands of the same number.
- An octopus cannot hold hands with two or more octopuses.
- A single hand cannot hold the hands of several octopuses.
All octopuses are polite, so they always follow the etiquette.
Among the N octopuses dancing ganggangsullae, choose one and call it octopus 1. Going clockwise from octopus 1, call the others octopus 2, 3, 4, ..., N. We will build a sequence of length N from the numbers of the hands held by adjacent pairs of octopuses. The number of the hand held by octopus 1 and octopus 2 is the 1st term, the number of the hand held by octopus 2 and octopus 3 is the 2nd term, ..., the number of the hand held by octopus N - 1 and octopus N is the (N - 1)th term, and the number of the hand held by octopus N and octopus 1 is the Nth term.
Given the number of octopuses N, find the lexicographically smallest sequence that can be made this way. The following is how to make 1 2 1 2, the lexicographically smallest sequence when there are 4 octopuses.

Input
The number of octopuses N (4 ≤ N ≤ 1,000) is given.
Output
Print the lexicographically smallest sequence of length N that can be made with N octopuses.
Print the numbers of the sequence in order, separated by spaces.
Hint
For two sequences of the same length, compare terms with the same index starting from the first index; the sequence whose smaller number appears first is lexicographically smaller.
For example, take the sequences A = {7, 3, 5} and B = {7, 4, 1}. A1 and B1 are both 7, so they are equal. A2 and B2 are 3 and 4, which differ, and since A2 is smaller, sequence A is lexicographically smaller than sequence B.