#P13023. [GCJ 2021 Qualification] Reversort Engineering

    ID: 12808 Type: RemoteJudge 10000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 3 Uploaded By: Tags>贪心2021Special JudgeGoogle Code Jam

[GCJ 2021 Qualification] Reversort Engineering

题目描述

Note: The main parts of the statements of the problems "Reversort" and "Reversort Engineering" are identical, except for the last paragraph. The problems can otherwise be solved independently.

Reversort is an algorithm to sort a list of distinct integers in increasing order. The algorithm is based on the "Reverse" operation. Each application of this operation reverses the order of some contiguous part of the list.

The pseudocode of the algorithm is the following:

Reversort(L):
  for i := 1 to length(L) - 1
    j := position with the minimum value in L between i and length(L), inclusive
    Reverse(L[i..j])

After i1i - 1 iterations, the positions 11, 22, \ldots, i1i - 1 of the list contain the i1i - 1 smallest elements of LL, in increasing order. During the ii-th iteration, the process reverses the sublist going from the ii-th position to the current position of the ii-th minimum element. That makes the ii-th minimum element end up in the ii-th position.

For example, for a list with 44 elements, the algorithm would perform 33 iterations. Here is how it would process L=[4,2,1,3]L = [4, 2, 1, 3]:

  1. i=1i = 1, j=3L=[1,2,4,3]j = 3 \longrightarrow L = [1, 2, 4, 3]
  2. i=2i = 2, j=2L=[1,2,4,3]j = 2 \longrightarrow L = [1, 2, 4, 3]
  3. i=3i = 3, j=4L=[1,2,3,4]j = 4 \longrightarrow L = [1, 2, 3, 4]

The most expensive part of executing the algorithm on our architecture is the Reverse operation. Therefore, our measure for the cost of each iteration is simply the length of the sublist passed to Reverse, that is, the value ji+1j - i + 1. The cost of the whole algorithm is the sum of the costs of each iteration.

In the example above, the iterations cost 33, 11, and 22, in that order, for a total of 66.

You are given a size NN and a cost CC. Find a list of NN distinct integers between 11 and NN such that the cost of applying Reversort to it is exactly CC, or say that there is no such list.

输入格式

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} lines follow. Each line describes a test case with two integers N\mathbf{N} and C\mathbf{C}, the size of the wanted list and the desired cost, respectively.

输出格式

For each test case, if there is no list of size N\mathbf{N} such that applying Reversort to it costs exactly C\mathbf{C}, output one line containing Case #xx: IMPOSSIBLE, where xx is the test case number (starting from 1). Otherwise, output one line containing Case #xx: y1y_1 y2y_2 \ldots yNy_{\mathbf{N}}, where xx is the test case number (starting from 1) and each yiy_i is a distinct integer between 1 and N\mathbf{N}, representing the ii-th element of one such possible list.

If there are multiple solutions, you may output any one of them.

5
4 6
2 1
7 12
7 2
2 1000
Case #1: 4 2 1 3
Case #2: 1 2
Case #3: 7 6 5 4 3 2 1
Case #4: IMPOSSIBLE
Case #5: IMPOSSIBLE

提示

Sample Explanation

Sample Case #1 is described in the statement above.

In Sample Case #2, the algorithm runs for only one iteration on the proposed output. In that iteration, reverse is applied to a sublist of size 1, therefore, its cost is 1.

In Sample Case #3, the first iteration reverses the full list, for a cost of 7. After that, the list is already sorted, but there are 5 more iterations, each of which contributes a cost of 1. Another valid output would be 7 5 4 3 2 1 6. For that output, the first iteration has a cost of 6, the last one has a cost of 2, and all others have a cost of 1.

In Sample Case #4, Reversort will necessarily perform 6 iterations, each of which will have a cost of at least 1, so there is no way the total cost can be as low as required.

Limits

  • 1T1001 \leq \mathbf{T} \leq 100.
  • 1C10001 \leq \mathbf{C} \leq 1000.

Test Set 1 (7 Pts, Visible Verdict)

  • 2N72 \leq \mathbf{N} \leq 7.

Test Set 2 (11 Pts, Visible Verdict)

  • 2N1002 \leq \mathbf{N} \leq 100.