Official

C - ABS Permutation (LIS ver.) Editorial by evima


For simplicity, we will consider the case \(N\) is odd. (When \(N\) is even, there are small differences, but the same main idea works.) Below, let \(M=\lfloor \frac{N}{2}\rfloor\).

An obvious upper bound for the answer is \(N-1\), only achieved by \(A=(1, 2, \ldots, N-1)\).

Considering the elements of this \(A\) from back to front, we can see that it has only two corresponding \(P\)’s:

\(P=(M+1,M,M+2,M-1,\ldots,1,N),(M+1,M+2,M,M+3,\ldots,N, 1)\).

If \(X=M+1\), we can print one of the above.

If \(X\neq M+1\), we have an upper bound of \(N-2\). Let us divide the elements other than \(X\) into two arrays \(L\) and \(R\), each of length \(M\), so that \(L_1<L_2<\ldots<L_M<R_1<R_2<\ldots<R_M\).

Now the upper bound is achieved by \(P=(X, L_M,R_1,L_{M-1},R_2,\ldots,L_1,R_M)\).

posted:
last update: