Find the lexicographically smallest shortest sequence of distinct labels a1..aN where every pair of labels appears adjacent somewhere, an Eulerian circuit of the complete graph.
Medium7GraphGreedyImplementationCombinatoricsNo attempts yetTime limit1sMemory limit512 MBThe statement that N given numbers are all different can be written as a single expression that uses the symbol != several times. For example, A, B, C are all different exactly when (A != B) && (B != C) && (C != A) holds, and the one-line form of that,
A != B != C != A
is called a one-line notation for A, B, C.
Writing the condition that five numbers A, B, C, D, E are all different as
A != B != C != D != E
is not a valid one-line notation. Five numbers are all different only when each of the 10 pairs is said to be different, so at least 10 copies of != are needed. In general, a one-line notation for N numbers needs at least C(N,2) copies of !=, where C(N,2) is the number of ways to choose 2 objects out of N distinct ones.
More precisely, a one-line notation for a1, a2, ..., aN is a sequence x1, x2, ..., xk of those names, written on one line with != between every two neighbours. For every pair of distinct names u, v there must be a position t with xt=u and xt+1=v, or with xt=v and xt+1=u. The length of the notation is the number of != symbols, that is k−1.
You are given an odd number N. Print the shortest one-line notation for a1, a2, ..., aN, and among the shortest ones the lexicographically smallest. Write a single space in place of each !=. For N=3 the answer is a1 a2 a3 a1. The notation a3 a1 a2 a3 has the same length, but it comes later in lexicographic order, so it is not the answer.
Hint: C(N,2), the least number of != symbols, equals the number of edges of the complete graph on N vertices.
The first line contains an odd number N with 1<N<500.
Print on the first line the lexicographically smallest one among the shortest one-line notations. Write the names as a1, a2, and separate them with one space. Lexicographic order compares the sequences of subscripts from the front, and subscripts compare as numbers, so a2 comes before a10.