Permutation
Limits: 2 sec., 256 MiB
This statement was translated automatically from Ukrainian.
Now that Zenik has guessed his password for Algotester on the seventy-fourth try, he can solve a simple problem before bed, even from his phone.
Problem: find any permutation \((p_1, p_2, \ldots, p_n)\) of the sequence \((1, 2, \ldots, n)\) such that \[\sum_{i=1}^n |p_i - i|=|p_1-1|+|p_2-2|+\dots+|p_n-n|=k,\] or state that no such permutation exists.
Input
The single line contains two integers \(n\) and \(k\).
Output
In the first line, print YES or NO —
whether such a permutation exists or not.
If such a permutation exists, then in the second line print \(n\) integers \(p_i\) — the elements of the permutation that satisfy the condition.
Constraints
\(1 \le n \le 10^5\),
\(0 \le k \le 10^9\).
5 points: \(n \le 10\);
20 points: no additional constraints.
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 4 4 | YES 1 4 2 3 |
Notes
Permutation — an ordered set without repetitions of the numbers \(1, 2, \ldots, n\).
In the example \(|p_1-1|+|p_2-2|+|p_3-3|+|p_4-4|=|1-1|+|4-2|+|2-3|+|3-4|=0+2+1+1=4\).
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Permutation - Editorial
\[\sum_{i=1}^n |p_i - i| \equiv \sum_{i=1}^n p_i + \sum_{i=1}^n i \equiv \frac{n(n+1)}{2}+\frac{n(n+1)}{2} \equiv n(n+1) \equiv 0 \mod 2\]
So if \(k\) is odd, the answer is
NO.
If \(k \le 2 \cdot (n - 1)\), we can build the following permutation: \[\left(\underline{\frac{k}{2}+1}, 2, 3, \ldots, \frac{k}{2}-1, \frac{k}{2}, \underline{1}, \frac{k}{2}+2, \frac{k}{2}+3, \ldots, n - 1, n\right)\]
If \(k > 2 \cdot (n-1)\), we set
\(p_1 = n\) and \(p_n = 1\). \(|p_1-1|+|p_n-n|=2 \cdot (n-1)\). We
decrease \(k\) by \(2 \cdot (n - 1)\) and solve the smaller
problem on the subsequence \((2, 3, \ldots, n
- 1)\) with the same reasoning. If at some step we have used all
elements of the permutation but \(k\)
is still greater than 0, there is no solution and the answer is
NO.
An example for \(n=11\), \(k=44\).
\((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) \xrightarrow{\text{+20}} (\underline{11}, 2, 3, 4, 5, 6, 7, 8, 9, 10, \underline{1}) \xrightarrow{\text{+16}} (11, \underline{10}, 3, 4, 5, 6, 7, 8, 9, \underline{2}, 1) \xrightarrow{\text{+8}} (11, 10, \underline{7}, 4, 5, 6, \underline{3}, 8, 9, 2, 1)\).
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|