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
The required condition is
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
for every odd index i\ge3, make
If this holds, the zig-zag condition is automatically satisfied.
So we only need a permutation that enforces these inequalities.
Construction
set
so
for every even index i<n, set
for every odd index i\ge3, set
if is even, set
This directly gives
(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)