Skyline#12306

Skyline (Editorial)

Problem Author:

Hasan Saleh (MathModel)

Prepared by:

Hasan Saleh (MathModel)

Edited by:

Hasan Saleh (MathModel)

Solution

We are given a permutation of \{1,2,\dots,n\} and define

a_i=\frac{p_i}{i}.

The required condition is

a_1<a_2>a_3<a_4>a_5<\dots

i.e. even indices are local maximums and odd indices are local minimums.

Instead of comparing neighbors directly, we enforce a stronger invariant:

  • for every even index , make

    a_i>1,
  • for every odd index i\ge3, make

    a_i<1.

If this holds, the zig-zag condition is automatically satisfied.

So we only need a permutation that enforces these inequalities.

Construction

  • set

    p_1=1,

    so

    a_1=1.
  • for every even index i<n, set

    p_i=i+1, \qquad a_i=\frac{i+1}{i}>1.
  • for every odd index i\ge3, set

    p_i=i-1, \qquad a_i=\frac{i-1}{i}<1.
  • if is even, set

    p_n=n, \qquad a_n=1.

This directly gives

p=[1,\,3,\,2,\,5,\,4,\,7,\,6,\,\dots]

(with at the end if is even).

A valid construction exists for all n\ge1, so we never output .

Complexity .

for _ in range(int(input())):
    n=int(input())
    p=[0]*n
    for i in range(n):p[i]=i+1
    for i in range(1,n-1,2):
        p[i],p[i+1]=p[i+1],p[i]
    a=[p[i]/(i+1) for i in range(n)]
    for i in range(1,n-1):
        if(i%2==0):
            assert(a[i-1]>a[i] and a[i]<a[i+1])
        else:
            assert(a[i-1]<a[i] and a[i]>a[i+1])
    print(*p)