Sorting
InterviewTime limit0.5sMemory limit256 MB
Given N and M, construct a permutation of 1..N for which insertion sort performs exactly M shifts, or report that it is impossible.
- Level
Medium6 of 10
- Topics
- Greedy, Sorting, Array, Implementation
- Solved
- No attempts yet
Problem
Given a permutation of size , the code below sorts it. A permutation of size is a sequence in which each integer from to appears exactly once.
cnt = cnt + 1
a[i+1] = a[i]
i = i-1
a[i+1] = x
| Pseudocode |
When a permutation of length $N$ is sorted, cnt always lies between $0$ and $N \times (N-1)/2$, inclusive. Given two integers $N$ and $M$, find a permutation of length $N$ for which the value of cnt after sorting with the code above is $M$.
Input
The first line contains the number of test cases . Each test case is a single line containing and .
Output
For each test case, output on one line a permutation whose value of cnt is .
If more than one permutation satisfies the condition, print any one of them.