P20978 [HackerRank] Lena sort
普及
题目描述

Lena developed a sorting algorithm described by the following pseudocode:

lena_sort(array nums) {
if (nums.size <= 1) {
return nums;
}
pivot = nums[0];
array less;
array more;
for (i = 1; i < nums.size; ++i) {
// Comparison
if (nums[i] < pivot) {
less.append(nums[i]);
}
else {
more.append(nums[i]);
}
}
sorted_less = lena_sort(less);
sorted_more = lena_sort(more);
ans = sorted_less + pivot + sorted_more;

return ans;

}

We consider a comparison to be any time some $nums[i]$ is compared with $\textbf{pivot}$.

You must solve $\textit{q}$ queries where each query $\boldsymbol{i}$ consists of some $len_i$ and $c_i$. For each query, construct an array of $len_i$ distinct elements in the inclusive range between $\mbox{1}$ and $\mathbf{10^{9}}$ that will be sorted by $\textbf{lena_sort}$ in exactly $c_i$ comparisons, then print each respective element of the unsorted array as a single line of $len_i$ space-separated integers; if no such array exists, print -1 instead.

Input Format

The first line contains a single integer denoting $\textit{q}$ (the number of queries).

Each line $\boldsymbol{i}$ of the $\textit{q}$ subsequent lines contains two space-separated integers describing the respective values of $\mbox{len}_i$ (the length of the array) and $c_i$ (the number of comparisons) for query $\boldsymbol{i}$.

Constraints

$1\leq q\leq10^5$
$1\leq len_i\leq10^5$
$0\leq c_i\leq10^9$
$1\leq$ the sum of $\mbox{len}_i$ over all queries $\leq10^{6}$

Output Format

Print the answer to each query on a new line. For each query $\boldsymbol{i}$, print $\mbox{len}_i$ space-separated integers describing each respective element in an unsorted array that Lena's algorithm will sort in exactly $c_i$ comparisons; if no such array exists, print -1 instead.

Sample Input 0
2
5 6
5 100

Sample Output 0
4 2 1 3 5
-1

Explanation 0

We perform the following $q=2$ queries:

One array with $len=5$ elements is $[4,2,1,3,5]$. The sequence of sorting operations looks like this:

Run $\textbf{lena_sort}$ on $[4,2,1,3,5]$. Compare $pivot=4$ with $2$, $\mbox{1}$, $3$, and $5$ for a total of $\begin{array}{c}4\end{array}$ comparisons. We're then left with $less=[2,1,3]$ and $\textit{more=[5]}$; we only need to continue sorting $less$, as $\textit{more}$ is sorted with respect to itself because it only contains one element.
Run $\textbf{lena_sort}$ on $less=[2,1,3]$. Compare $pivot=2$ with $\mbox{1}$ and $3$ for a total of $2$ comparisons. We're then left with $less=[1]$ and $\textit{more}=[3]$, so we stop sorting.

We sorted $[4,2,1,3,5]$ in $4+2=6$ comparisons and $\boldsymbol{c=6}$, so we print 4 2 1 3 5 on a new line.

It's not possible to construct an array with $len=5$ elements that $\textbf{lena_sort}$ will sort in exactly $\boldsymbol{c=100}$ comparisons, so we print -1 on a new line.

Sample Input 1
3
1 0
4 6
3 2

Sample Output 1
1
4 3 2 1
2 1 3

Explanation 1

We perform the following $q=3$ queries:

We want an array with $len=1$ element that $\textbf{lena_sort}$ sorts in $\boldsymbol{c=0}$ comparisons; any array with $\mbox{I}$ element is already sorted (i.e., $\textbf{lena_sort}$ performs $\mbox{o}$ comparisons), so we choose $\left[\mbox{1}\right]$ as our array and print 1 on a new line.

One array with $len=4$ elements is $[4,3,2,1]$; sorting it with $\textbf{lena_sort}$ looks like this:

$\textbf{lena_sort}$ on $[4,3,2,1]$. Compare $pivot=4$ with $3$, $2$, and $\mbox{I}$ for a total of $3$ comparisons. We're then left with $less=[3,2,1]$ and $\textit{more}=[]$; we only need to continue sorting $less$, as $\textit{more}$ is empty.
Run $\textbf{lena_sort}$ on $less=[3,2,1]$. Compare $pivot=3$ with $2$ and $\mbox{I}$ for a total of $2$ comparisons. We're then left with $less=[1,2]$ and $\textit{more}=[]$, so we only continue sorting $less$.
Run $\textbf{lena_sort}$ on $less=[2,1]$. Compare $pivot=2$ with $\mbox{I}$ for a total of $\mbox{1}$ comparison. We then stop sorting, as $less=[1]$ and $\textit{more}=[]$.

We sorted $[4,3,2,1]$ in $3+2+1=6$ comparisons and $\boldsymbol{c=6}$, so we print 4 3 2 1 on a new line.

One array with $len=3$ elements is $[2,1,3]$. When we run $\textbf{lena_sort}$ on it, we compare $pivot=2$ with $\mbox{1}$ and $3$ for a total of $2$ comparisons. We're then left with $less=[1]$ and $\textit{more}=[3]$, so we stop sorting.

We sorted $[2,1,3]$ in $2$ comparisons and $\boldsymbol{c=2}$, so we print 2 1 3 on a new line.

输入格式

见题面

输出格式

见题面

样例输入
2 5 6 5 100
样例输出
4 2 1 3 5 -1