#P7452. [THUSC 2017] 换桌

[THUSC 2017] 换桌

Problem Description

During a class party, the homeroom teacher requires that, during dinner, students from the same dormitory must sit together to make management easier. However, after dinner, during entertainment time, students who like different games will gather together. This involves the problem of seat reassignment.

There are nn round tables in a row (numbered from left to right as 00 to n1n-1). Each table has mm seats (numbered counterclockwise as 00 to m1m-1). During dinner, every seat is occupied by one person. After dinner, everyone must choose a new seat (which may be the same as the original one). Specifically, the person at table ii, seat jj can only choose a new seat on some table between Li,jL_{i,j} and Ri,jR_{i,j} (inclusive), and it can be any seat on any of those tables. After all new seats are decided, everyone starts moving. The physical cost is computed as follows:

The movement has two steps:

  1. Move from the starting table to the corresponding seat on the target table. The cost is twice the distance between the two tables. That is, moving from table ii to the corresponding seat on table jj costs 2×ij2\times |i-j|.
  2. Move around the target table from the corresponding seat to the target seat. Since the table is round, the guest will choose the shorter direction. The cost is one times the moved distance. That is, moving from seat xx to seat yy costs min(xy,mxy)\min(|x-y|,m-|x-y|).

See the figure below for details:
pic1

Now, given each guest’s constraint (the interval of tables where their new seat may be), you need to design a plan to minimize the sum of physical costs of all guests. In this problem, assume guests do not affect each other while moving.

Input Format

Read from standard input.

The first line contains two integers nn and mm.

Next nn lines follow, each containing mm space-separated integers describing matrix LL. The jj-th number in the ii-th line is Li,jL_{i,j}.

Next nn lines follow, each containing mm space-separated integers describing matrix RR. The jj-th number in the ii-th line is Ri,jR_{i,j}.

The testdata is randomly generated. The pseudocode is:

for i <- 0 to n-1
    for j <- 0 to m-1
        L[i,j] <- independently and uniformly pick an integer from 0 to n-1
        R[i,j] <- independently and uniformly pick an integer from 0 to n-1
        if L[i,j] > R[i,j] then
            tmp <- L[i,j]
            L[i,j] <- R[i,j]
            R[i,j] <- tmp

Output Format

Write to standard output.

Output the minimum total physical cost. If there is no feasible plan, output no solution.

2 4
0 1 1 0
1 0 1 0
0 1 1 0
1 0 1 0
10
2 4
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
no solution
2 10
0 0 1 1 0 0 0 1 0 0
1 1 1 0 0 1 0 0 0 0
1 0 1 1 1 0 1 1 1 1
1 1 1 1 1 1 0 0 1 0
22

Hint

Sample Explanation

For sample 11:
pic2

Persons 00 and 33 at table 00, as well as person 00 and 22 at table 11, are restricted to sit only at their original table (not necessarily their original seat). Others need to move to table 11 and table 00, respectively.

It can be seen that the optimal plan is as shown above, and the total physical cost is 1010.

For sample 22, everyone wants to sit at table 00, so there is no feasible plan.

Constraints: 1n3001\le n\le 300, 1m101\le m\le 10, 0LiRin10\le L_i\le R_i\le n-1. | Test Point | nn\le | | :----------: | :----------: | | 1~2 | 22 | | | 3~8 | 4040 | | | 9~14 | 100100 | | | 15~20 | 300300 | |

Translated by ChatGPT 5