QOJ.ac

QOJ

時間限制: 5 s 記憶體限制: 512 MB 總分: 100

#15025. Changing Tables

统计

During a class party, the teacher requires students from the same dormitory to sit together while eating for easier management. However, after the meal, during entertainment time, students who enjoy different games gather together. This process involves the problem of seat allocation.

There are $n$ round tables arranged in a row (numbered $0$ to $n-1$ from left to right), and each table has $m$ seats (numbered $0$ to $m-1$ in counter-clockwise order). During the meal, every seat is occupied by one person. After the meal, everyone needs to choose a new seat (the new seat may be the same as the original one). Specifically, the new seat for the person at table $i$, seat $j$ must be chosen from any seat at any table in the range $[L_{i,j}, R_{i,j}]$. Once the new seats are determined, everyone begins to move. The physical exertion for moving is calculated according to the following rules:

The process of moving seats is divided into two steps:

  1. Moving from the starting table to the corresponding seat at the target table. The physical exertion for this process is twice the distance between the two tables, i.e., the exertion to move from table $i$ to table $j$ at the corresponding seat is $2 \times |i-j|$.

  2. Moving from the corresponding seat at the target table to the target seat around the table. Since the table is round, guests will choose the shortest direction to move. The physical exertion is equal to the distance moved, i.e., the exertion to move from seat $x$ to seat $y$ is $\min(|x-y|, m-|x-y|)$.

Details are shown in the figure below:

Now, given the constraints for each guest (the range of tables where their new seat must be located), you need to design a plan that minimizes the total physical exertion of all guests. In this problem, assume that guests do not interfere with each other while moving.

Input

The input is read from standard input.

The first line contains two integers $n$ and $m$.

The next $n$ lines each contain $m$ space-separated integers describing the matrix $L$: the $j$-th number in the $i$-th row represents $L_{i,j}$.

The next $n$ lines each contain $m$ space-separated integers describing the matrix $R$: the $j$-th number in the $i$-th row represents $R_{i,j}$.

The data is randomly generated. The pseudocode for generating the data is as follows:

for i <- 0 to n-1
    for j <- 0 to m-1
        L[i,j] <- independently and uniformly choose an integer from 0 to n-1
        R[i,j] <- independently and uniformly choose 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

Output to standard output.

Output the minimum total physical exertion. If no valid plan exists, output no solution.

Examples

Input 1

2 4
0 1 1 0
1 0 1 0
0 1 1 0
1 0 1 0

Output 1

10

Note

The guests at seat $0$ and $3$ of table $0$, and seat $0$ and $2$ of table $1$ are restricted to sitting at their original tables (not necessarily the original seats), while the others need to move to table $1$ and table $0$ respectively.

It can be seen that the optimal plan is as shown in the figure above, with a total physical exertion of $10$.

Input 2

2 4
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

Output 2

no solution

Note

Everyone wants to sit at table $0$, so there is no valid plan.

Input 3

See ex_3.in and ex_3.ans in the download directory.

Output 3

See ex_3.in and ex_3.ans in the download directory.

Input 4

See ex_4.in and ex_4.ans in the download directory.

Output 4

See ex_4.in and ex_4.ans in the download directory.

Subtasks

For all data: $1 \le n \le 300$, $1 \le m \le 10$, $0 \le L_{i,j} \le R_{i,j} \le n-1$.

Test Case $n$ $m$
1, 2 $1 \le n \le 2$ $1 \le m \le 10$
3, 4, 5, 6, 7, 8 $1 \le n \le 40$
9, 10, 11, 12, 13, 14 $1 \le n \le 100$
15, 16, 17, 18, 19, 20 $1 \le n \le 300$

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.