Official

E - Nearer Permutation Editorial by evima


For convenience, both the indices and values are \(0\)-based below.

First, let us consider how to find \(f(x)\).

Let \(z=x\) be the initial solution, and let us try to decide the terms in \(z\) from left to right. Under the condition \(d(x,z) \leq d(y,z)\), we should move a value that is as small as possible to the beginning. Eventually, we have the following algorithm (called Algorithm A below), where \(Invs\) is the inversion number of \(x\).

  • Let \(result=(),s=Invs/2\).
  • Perform the operation below (called Operation X below) \(N\) times.
    • Let \(x_m\) be the smallest of \(x_0,x_1,\cdots,x_{\min(floor(s),N-1)}\).
    • Append \(x_m\) to the end of \(result\).
    • Delete \(x_m\) from \(x\) (and reindex the remaining terms).
    • Let \(s:=s-m\).
  • Return \(result\).

Take the smallest index \(k\) such that \(A_k>A_{k+1}\). If there is no such \(k\), the answer is obviously Yes.

From the way Operation X is executed, one can see that the \(x\) after performing Operation X \(k\) times has the following properties.

  • The \(0\)-th term of \(x\) is \(A_k\), and the \((floor(s)+1)\)-th term is \(A_{k+1}\). Additionally, \(\min(x_0,x_1,\cdots,x_{floor(s)})=x_0\).

  • If \(A_{k+1}\) is removed from \(x\), \(A_k,A_{k+2},A_{k+3},\cdots,A_{N-1}\) will line up in this order.

Therefore, it can be seen that the permutations \(x\) that satisfy \(f(x)=A\) are limited to the ones obtained by the following procedure.

  • Let \(B=A\).
  • For \(i=k+1,k-1,k-2,\cdots,0\), do the following operation.
    • Move \(B_i\) to the right by some terms. More precisely, change \(B_i,B_{i+1},\cdots,B_{i+t}\) to \(B_{i+1},\cdots,B_{i+t},B_i\), where \(t\) is the length of the move. Here, \(B_i < B_{i+j}\) (\(1 \leq j \leq t\)) must hold (before the operation). Additionally, \(A_k < B_{i+j}\) must hold if \(i=k+1\).

For each \(i\), let \(c_i\) be the length of the move of \(B_i\). One should adjust \(c_i\) so that Algorithm A will return the desired output.

Consider the difference \(v=s-p\) of the position \(p\) of the value \(A_{k+1}\) and the value \(s\) after performing Operation X \(k+1\) times.

First, consider the case \(i \neq k+1\). If \(c_i\) increases by \(1\), the initial value of \(s\) will increase by \(1/2\), and the value of \(m\) chosen in the \(i\)-th operation will increase by \(1\), so \(s\) will decrease by \(1/2\) in total, and \(v\) will decrease by \(1/2\) in turn.

In the case \(i = k+1\), the initial value of \(s\) will increase by \(1/2\), and the value of \(p\) increases by \(1\), so \(v\) will decrease by \(1/2\).

After all, \(v\) will decrease by \(1/2\) in either case. Let \(S\) be the inversion number of \(A\). If all \(c_i\) are \(0\), we have \(v=S/2\), and the desired value for \(v\) is \(0\) or \(1/2\), so we can see that the sum of \(c_i\) should be \(S\) or \(S-1\).

Now, let us try both of these values. (Actually, it can be shown that it is enough just to consider \(S-1\).)

Since the sum of \(c_i\) is known, the inversion number of \(x\) is also known. Here, if one decides the value of \(c_i\) in ascending order of \(A_i\), a greedy strategy to make them as large as possible works successfully. This is because one could transform a solution that does not accord with this strategy into one that does. We omit the detail of the proof of this, which is a simple case-by-case analysis.

If there is a contradiction while deciding \(c_i\) according to the above strategy, or the sum does not reach the predefined target, the answer is No; otherwise, it is Yes.

The time complexity of this method is \(O(N \log N)\), the bottleneck being the computation of the inversion number.

Sample code (c++)

posted:
last update: