#P13124. [GCJ 2019 Finals] Sorting Permutation Unit

    ID: 12908 Type: RemoteJudge 20000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 6 Uploaded By: Tags>2019Special Judge排序Ad-hocGoogle Code Jam

[GCJ 2019 Finals] Sorting Permutation Unit

题目描述

You may have heard of Google's Tensor Processing Units, which are used to build neural networks. However, there is one research area that is even deeper and more important than machine learning: sorting!

We are working on a special new chip called the Sorting Permutation Unit, which is very fast at applying permutations to arrays of integers. Formally, a permutation is an ordering of the first n\mathbf{n} positive integers

$$\mathbf{p}_{1}, \mathbf{p}_{2}, \ldots, \mathbf{p}_{\mathbf{n}} $$

and applying it to an array of n\mathbf{n} integers

$$\mathbf{a}_{1}, \mathbf{a}_{2}, \ldots, \mathbf{a}_{\mathbf{n}} $$

yields the new array

$$\mathbf{a}_{\mathbf{p}_{1}}, \mathbf{a}_{\mathbf{p}_{2}}, \ldots, \mathbf{a}_{\mathbf{p}_{\mathbf{n}}} $$

For example, applying the permutation 3, 1, 2, 4 to the array 99, 234, 45, 800 would yield the new array 45, 99, 234, 800.

However, permutations are expensive to represent in the hardware, so the unit can only have access to at most P\mathbf{P} distinct permutations. We need your help figuring out what those permutations should be!

Given K\mathbf{K} arrays of N\mathbf{N} integers each, you must first specify up to P\mathbf{P} permutations (of size N\mathbf{N}) of your choice. Then, for each of those K\mathbf{K} input arrays, you must provide one sequence of up to S\mathbf{S} instructions (each of which is a permutation from your specified set). When the instructions in this sequence are applied, in the given order, to the array, they must yield an array sorted in nondecreasing order. In each of your K\mathbf{K} sequences of instructions, you may use each of your P\mathbf{P} permutations zero or more times (not necessarily consecutively).

输入格式

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} test cases follow. Each begins with one line with four integers P\mathbf{P}, S\mathbf{S}, K\mathbf{K}, and N\mathbf{N}: the maximum number of permutations allowed, the maximum number of instructions you are allowed to use to sort each array, the number of arrays, and the number of integers in each array. Then, there are K\mathbf{K} more lines of N\mathbf{N} integers Ai1\mathbf{A}_{\mathbf{i}1}, Ai2\mathbf{A}_{\mathbf{i}2}, ..., AiN\mathbf{A}_{\mathbf{iN}} each, where the j-th integer on the i-th line, Aij\mathbf{A}_{\mathbf{ij}}, represents the j-th value of the i-th array.

输出格式

For each test case, first output the following, in this order:

  • One line containing Case #x:, where x\mathbf{x} is the test case number (starting from 1).
  • One line containing one integer P\mathbf{P}', where 1PP1 \leq \mathbf{P}' \leq \mathbf{P}: the number of permutations you have chosen to use.
  • P\mathbf{P}' lines, the i-th of which contains N\mathbf{N} integers pi1\mathbf{p}_{\mathbf{i}1} pi2\mathbf{p}_{\mathbf{i}2} ... piN\mathbf{p}_{\mathbf{iN}}, where pij\mathbf{p}_{\mathbf{ij}} is the j-th element of the i-th permutation.

Then, output K\mathbf{K} more lines. The i-th of these gives the instructions that you will apply to the i-th array given in the input. Each such line must begin with one integer S\mathbf{S}', where 0SS0 \leq \mathbf{S}' \leq \mathbf{S}, and must continue with S\mathbf{S}' integers X1\mathbf{X}_{1}, X2\mathbf{X}_{2}, ..., XS\mathbf{X}_{\mathbf{S}'}, where 1XkP1 \leq \mathbf{X}_{\mathbf{k}} \leq \mathbf{P}' for all k\mathbf{k}. Here, Xk\mathbf{X}_{\mathbf{k}} represents that the k-th instruction you apply to the i-th array is the Xk\mathbf{X}_{\mathbf{k}}-th permutation (counting starting from 1) in your list of permutations. These instructions must yield an array with the elements of the i-th input array, sorted in nondecreasing order.

2
20 450 4 3
10 10 11
17 4 1000
999 998 997
10 10 11
20 450 5 5
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4
Case #1:
2
3 1 2
2 1 3
0
1 2
2 2 1
1 2
Case #2:
1
5 1 2 3 4
0
1 1
2 1 1
3 1 1 1
4 1 1 1 1

提示

Sample Explanation

In Sample Case #1, we can define up to P=20\mathbf{P} = 20 permutations. One viable strategy uses only these two:

  1. 3 1 2
  2. 2 1 3

We can handle the four arrays as follows:

  • 10 10 11: This is already sorted in nondecreasing order, so we do not need to do anything.
  • 17 4 1000: We can apply permutation #2 to yield 4 17 1000.
  • 999 998 997: One option is to first apply permutation #2 to turn the array into 998 999 997, then apply permutation #1 to turn the array into 997 998 999.
  • 10 10 11: This is the same as the first array. Applying permutation #2 also yields array sorted in nondecreasing order. (But we could have used the line 0 as we did before.)

In Sample Case #2, notice that we can use the same permutation instruction more than once on the same array, if desired.

Limits

  • 1T101 \leq \mathbf{T} \leq 10.
  • S=450\mathbf{S} = 450.
  • 1K301 \leq \mathbf{K} \leq 30.
  • 2N502 \leq \mathbf{N} \leq 50.
  • 1Aij10001 \leq \mathbf{A}_{\mathbf{ij}} \leq 1000, for all i\mathbf{i} and j\mathbf{j}.

Test set 1 (5 Pts, Visible)

  • P=20\mathbf{P} = 20.

Test set 2 (22 Pts, Hidden)

  • P=5\mathbf{P} = 5.