#P3782. [WC2017] 排序

    ID: 2718 远端评测题 7500ms 500MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2017WC/CTSC/集训队提交答案Special Judge

[WC2017] 排序

Description

Niuniu has recently learned about sorting and has become very interested in the efficiency of sorting algorithms. To optimize sorting time, Niuniu turned to you at the winter camp to help design a computer to perform sorting (ascending).

The computer is designed as follows:

  • The sequence to be sorted is stored in an array QQ of size nn.

  • The smallest unit of computation is a comparator, represented by a triple (i,j,t)(i, j, t) (all integers), where 1i<jn1 \le i < j \le n, 1tm1 \le t \le m. Its function is: at time tt, compare QiQ_{i} and QjQ_{j}. If Qi>QjQ_{i} > Q_{j}, swap QiQ_{i} and QjQ_{j}; otherwise do nothing.

  • For the computer to work (but not necessarily solve the problem), any two comparators must not conflict. Two comparators (A,B,S)(A, B, S) and (C,D,T)(C, D, T) conflict if and only if at least two numbers among A,B,C,DA, B, C, D are equal and S=TS = T. For example, comparators (1,3,1)(1, 3, 1) and (2,4,1)(2, 4, 1) do not conflict, while (1,2,3)(1, 2, 3) and (2,3,3)(2, 3, 3) do conflict.

At runtime, each comparator in the computer performs its operation at its preset time. The running time of the computer is the maximum value ww of parameter tt among all comparators; the smaller this value, the shorter the running time.

To test your computer design, Niuniu provides testdata. Each group of testdata contains qq queries, i.e., qq permutations PP of length nn. For each group of testdata, please design a computer with running time at most mm (otherwise the computer will time out) that can sort all PP in the testdata.

Input Format

To help you distinguish sub-tasks, each input file begins with an integer II, the current test point index.

The second line contains an integer JJ, the number of testdata groups in this test point.

For each group of testdata, the first line contains three integers q,n,mq, n, m.

Then follow qq lines, each containing nn integers describing a permutation PP of 11 to nn.

It is guaranteed that a solution exists.

Output Format

For each group of testdata, output an integer ff in the first line, the number of comparators in your computer.

Then output ff lines, each with 33 integers i,j,ti, j, t, describing a comparator (i,j,t)(i, j, t) of your computer.

If i,j[1,n]i, j \notin [1, n], then the computer cannot work.

0
1
3 5 4
1 2 5 3 4
3 4 5 1 2
3 1 4 2 5
7
1 2 1
3 4 1
2 3 2
4 5 2
1 2 3
3 4 3
2 3 4

Hint

Download input data and checker

Download from here.

Warning: All files (including the SPJ) are in Windows (64-bit) format.

About the UKE issue

Because the SPJ has high time complexity, please use as few comparators as possible. Otherwise the SPJ may TLE, causing the verdict to be UKE.

The SPJ time complexity is:

$$O\left ( \sum_{i=1}^{J}q_i\times (n_i+f_i) \right )$$

Scoring

There are 8 test points with 6,12,13,10,10,10,19,206, 12, 13, 10, 10, 10, 19, 20 groups of testdata respectively, 100 groups in total, and each group of testdata is worth 1 point.

In each group of testdata, if 1wm1 \le w \le m and f106f \le 10^{6}, the comparators do not conflict, and the computer can sort all PP in the testdata, then that testdata scores; otherwise it scores 0.

If you output fewer than JJ computers, we assume your ii-th computer corresponds to the ii-th group of testdata.

If your output format is invalid, we do not guarantee you can get points for that test point.

Data characteristics

For 100%100\% of the data, 1m1501 \le m \le 150, J{1,6,10,12,13,19,20}J \in \{1, 6, 10, 12, 13, 19, 20\}, 0I80 \le I \le 8. See the downloadable data for the rest.

Some partial characteristics:

  • Test point 44 (10 points): For every input permutation PP, all its cycle sizes are pairwise distinct. You can think of a permutation PP of length nn as an undirected graph with nn nodes and nn edges (a functional graph consisting only of cycles), where there is an edge between node ii and node PiP_{i}. Every connected component in this graph is a cycle (i.e., the graph consists of several cycles), e.g., $P=\{\color{blue}1,\color{green}3,2,\color{purple}6,\color{red}10,\color{purple}7,4,\color{red}5,8,9\color{black}\}$ (each color indicates a cycle).

  • Test point 77 (19 points): For every input permutation PP, there exist several pairwise disjoint intervals [li,ri][l_i, r_i] such that after rotating each interval [li,ri][l_i, r_i] left by one step, the array becomes sorted, e.g., $P=\{\color{blue}3,1,2,\color{green}7,4,5,6,\color{purple}8,\color{red}9\color{black}\}$ (each color indicates an interval). Example of a left cyclic shift: (1,2,3,4)(1, 2, 3, 4) rotated left by one step becomes (2,3,4,1)(2, 3, 4, 1).

  • In the first 8 groups of testdata of test point 77 (8 points), for every input permutation PP, there exists an integer ii such that after rotating the interval [1,i][1, i] left by one step, the array becomes sorted, e.g., P={5,1,2,3,4,6,7,8}P=\{\color{red}5,1,2,3,4\color{black},6,7,8\}.

Test your output

We provide a checker (Windows x64) in the files to evaluate your output. Usage:

checker.exe sort<I>.in sort<I>.out sort<I>.out

Here II is the test point index (do not include angle brackets in the input), checker.exe is the checker, sort<I>.in is the provided input file, and sort<I>.out is your corresponding output file (input twice). Running this command will show your score on the input file sort<I>.in with your output file sort<I>.out.

After you invoke the program, the checker prints the result for each group of testdata, including (if multiple errors occur simultaneously, one of them is reported):

Result Meaning
FAIL! Unknown error: SPJ runtime failure.
Input error! Invalid input file: you modified the input.
The number of the comparators is invalid! Detected f>106f>10^6.
Unexpected EOF The computer you provided is incomplete.
The running time of the comparator should be in [1, 150] Detected w[1,150]w \notin [1, 150].
Invalid sorting network! The computer you provided cannot work.
The answer is incorrect The computer you provided cannot solve all queries.
Invalid! m=(X) but M=(X) The computer timed out: w=Y,m=Xw=Y, m=X (Y>XY>X).
Correct! m=(X) and M=(X) The computer is correct: w=Y,m=Xw=Y, m=X (YXY \le X).

Finally, if the checker finishes normally, it additionally prints Total points: (Z), which is the total points you scored on this test point.

Reminder

Please pay attention to the difference between “test point”, “testdata”, and “query”.

Please keep in mind the structure below:

  • The whole problem

    • Test point 11

      • Testdata 1.11.1

        • Query 1.1.11.1.1
      • Testdata 1.21.2

        • Query 1.2.11.2.1
      • Testdata 1.31.3

        • Query 1.3.11.3.1

        • Query 1.3.21.3.2

        • Query 1.3.51.3.5

      • Testdata 1.61.6

    • Test point 22

    • Test point 88

Translated by ChatGPT 5