No.1635 Let’s Sort Integers!!
Problem Statement
You are given positive integers $N,K$. Determine whether there exists a permutation $P$ obtained by rearranging $1,2,\ldots, N$ that satisfies the following condition, and if one exists, construct one such example.
- $|P_1-P_2|+|P_2-P_3|+\cdots+|P_{N-1}-P_N|=K$
Input
$N\ K$
- $2\leq N\leq 10^6$
- $1\leq K\leq10^{18}$
- All inputs are integers.
Output
If no permutation $P$ satisfying the condition exists, output -1.
If a permutation $P$ satisfying the condition exists, output $1$ such permutation in the following format.
$P_1\ P_2\ \ldots\ P_N$
Sample
Sample 1
Input
4 5
Output
2 1 4 3
This satisfies the condition since $|2-1|+|1-4|+|4-3|=5$. Other outputs such as 1 4 3 2 and 4 2 3 1 also satisfy the condition.
Sample 2
Input
2 1
Output
1 2
This satisfies the condition since $|1-2|=1$. 2 1 also satisfies the condition.
Sample 3
Input
3 1
Output
-1
No permutation $P$ satisfying the condition exists.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
とりゐ
logx
re_re0101