Aerobics (Large)

Time limit5sMemory limit512 MB

Summary
Sort students by reach, largest first, then place them line by line on the mat following the prescribed packing rule.
Level

Easy3 of 10

Topics
Simulation, Sorting
Solved
No attempts yet

Problem

The aerobics class begins. The trainer asks the students to stand on the mat so that everybody can swing her arms freely without hitting anybody else. The students keep milling around, so the trainer asked for a program that assigns the positions instead.

The mat is a rectangle of width WW and length LL. Student ii needs a circle of radius rir_i around the point where she stands, where rir_i is the reach of her arms. Two circles may touch but may not overlap, so the distance between the centers of student ii and student jj has to be at least ri+rjr_i + r_j. Every center has to lie on the mat, that is 0≤xi≤W0 \le x_i \le W and 0≤yi≤L0 \le y_i \le L. The arms may reach outside the mat.

The mat is roomy. Its area is at least five times the total area of the circles, so 5π(r12+⋯+rN2)≤W⋅L5\pi(r_1^2 + \cdots + r_N^2) \le W \cdot L holds and a valid placement always exists.

Many placements are valid, so this problem accepts only the one placement produced by the rule given in the output section.

Input

The first line contains the number of test cases TT. Each test case consists of two lines. The first line contains the number of students NN, the width WW of the mat, and the length LL of the mat, separated by spaces. The second line contains NN integers r1,…,rNr_1, \ldots, r_N, where rir_i is the reach of the arms of student ii.

The constraints are the following.

  • 1≤T≤501 \le T \le 50
  • 1≤N≤10001 \le N \le 1000
  • 1≤W,L≤1091 \le W, L \le 10^9
  • 1≤ri≤1051 \le r_i \le 10^5
  • 5π(r12+⋯+rN2)≤W⋅L5\pi(r_1^2 + \cdots + r_N^2) \le W \cdot L
  • The sum of NN over all test cases is at most 6000.

Output

For each test case print one line that starts with Case #n: and continues with 2N2N integers separated by single spaces. Here nn is the number of the test case, counting from 1, and the integers are x1x_1, y1y_1, x2x_2, y2y_2 and so on in the input order, where (xi,yi)(x_i, y_i) is the point at which student ii stands.

The placement is fixed by the following rule.

First sort the students by reach, largest first. Students with the same reach keep their input order. Call the result the placement order.

If W≥LW \ge L, the students are grouped into vertical lines. The students of one line share the same xx and get increasing yy. In that case the length of a line is bounded by S=LS = L, the coordinate ff that moves inside a line is yy, and the coordinate gg that moves from line to line is xx. If W<LW < L, the two axes are exchanged and the lines are horizontal. The students of one line share the same yy and get increasing xx, with S=WS = W, f=xf = x and g=yg = y.

Take the students one at a time in placement order and add each to the current line.

  • The first line has g=0g = 0.
  • The first student of a line stands at f=0f = 0.
  • Any other student ii is placed relative to the student pp placed just before her. If pp has reach rpr_p at coordinate fpf_p and fp+rp+ri≤Sf_p + r_p + r_i \le S, then student ii joins the same line at f=fp+rp+rif = f_p + r_p + r_i.
  • Otherwise student ii opens a new line. If RR is the reach of the first student of the line being closed, the new line has gg equal to the gg of the closed line plus R+riR + r_i, and student ii stands at f=0f = 0 of that new line.

Every coordinate this rule produces is an integer, and under the constraints of the problem every coordinate stays on the mat.

Examples3

  1. Example 1

    Input
    2
    2 6 6
    1 1
    3 320 2
    4 3 2
    
    Expected output
    Case #1: 0 0 0 2
    Case #2: 0 0 7 0 12 0
    
  2. Example 2

    Input
    1
    1 4 4
    1
    
    Expected output
    Case #1: 0 0
    
  3. Example 3

    Input
    1
    5 30 30
    3 3 3 3 3
    
    Expected output
    Case #1: 0 0 0 6 0 12 0 18 0 24