#P12976. 受力分析 Force

受力分析 Force

Background

Comentropy, who was still writing problems, suddenly received a challenge—surprisingly, it was a mechanics problem. He was shocked, but once he saw the Constraints, he immediately knew how to solve it. However, the process was long and tedious, so he asked you to help simplify it.

Note: This problem has no strict physical theory behind it, and it does not require any physics background.

It is recommended to read the formal statement.

Problem Description

There are n×nn \times n cubic blocks arranged in a square grid. Each block has its own weight, and you need to keep them balanced and stationary. This requires that for the block in row ii and column jj, the supporting force on its bottom face must lie within the interval [li,j,ri,j][l_{i,j}, r_{i,j}].

The problem setters provide nn steel wires in the horizontal direction and nn steel wires in the vertical direction to provide the supporting force, and place the blocks at the wire intersection points. Now you can apply forces to these 2n2n wires separately. (Note that the force is the same everywhere along the same wire.)

Definition: Numerically, the supporting force received by a block equals the sum of the forces you apply to the two wires beneath it.

As shown in the figure, one set of wires is placed horizontally, with applied forces [x1,x2,,xn][x_1, x_2, \dots, x_n]; the other set is placed vertically, with applied forces [y1,y2,,yn][y_1, y_2, \dots, y_n]. The resultant force at intersection (i,j)(i, j) is xi+yjx_i + y_j.

Please find the force applied to each wire. To make it harder for Comentropy, the setters also require the sequence [x1,x2,,xn,y1,y2,,yn][x_1, x_2, \dots, x_n, y_1, y_2, \dots, y_n] to be lexicographically smallest. Can you help him handle this problem?

Each force must be a non-negative integer, pointing upward.

Formally: Given two n×nn \times n matrices l,rl, r, find non-negative integer sequences {x}\{x\} and {y}\{y\} such that li,jxi+yjri,jl_{i,j} \leq x_i + y_j \leq r_{i,j}. Among all solutions, output the one that makes the combined sequence [x1,x2,,xn,y1,y2,,yn][x_1, x_2, \dots, x_n, y_1, y_2, \dots, y_n] lexicographically smallest.

Input Format

The first line contains a positive integer n(1n500)n(1 \leq n \leq 500), indicating an n×nn \times n matrix.

Lines 22 to n+1n+1 contain a matrix representing the lower bounds ll.

Lines n+2n+2 to 2n+12n+1 contain another matrix representing the upper bounds rr.

Output Format

If there is no solution, output -1 directly.

Otherwise:

The first line contains nn numbers, representing the forces on the horizontal wires x1,,xnx_1, \ldots, x_n.

The second line contains nn numbers, representing the forces on the vertical wires y1,,yny_1, \ldots, y_n.

Note: If there are multiple answers, output the lexicographically smallest one.

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

Hint

Sample 1 Explanation: There may be another solution with x1=2,x2=2,y1=0,y2=3x_1 = 2, x_2 = 2, y_1 = 0, y_2 = 3, but {0,0},{2,5}\{0, 0\}, \{2, 5\} is a lexicographically smaller solution, and it can be proven to be lexicographically smallest.

For 10%10\% of the testdata, it is guaranteed that 1n7,0ri,j71 \leq n \leq 7, 0 \leq r_{i,j} \leq 7.

For 20%20\% of the testdata, it is guaranteed that 1n501 \leq n \leq 50, 1li,jri,j2001 \leq l_{i,j} \leq r_{i,j} \leq 200.

For 40%40\% of the testdata, it is guaranteed that 1n2001 \leq n \leq 200.

For the remaining 20%20\% of the testdata, it is guaranteed that li,j=ri,jl_{i,j} = r_{i,j}.

For 100%100\% of the testdata, it is guaranteed that 1n5001 \leq n \leq 500, 0li,jri,j1090 \leq l_{i,j} \leq r_{i,j} \leq 10^9.

Please pay attention to the impact of constants on the running efficiency of your program.

Translated by ChatGPT 5