#P9826. [ICPC2020 Shanghai R] Rice Arrangement

    ID: 9093 Type: RemoteJudge 1000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 5 Uploaded By: Tags>数学贪心2020上海O2优化ICPC

[ICPC2020 Shanghai R] Rice Arrangement

题目描述

Wowo is a hospitable Xinjiang uncle. kk guests will have Uyghur Polo (a traditional Uyghur food) in Wowo's house around a big round table. nn (nkn\ge k) chairs are placed around the table uniformly. Each guest sits on a chair and no two guests sit on the same chair. kk bowls of Uyghur Polo are on the table. Each bowl is next to some chair (with or without\textbf{with or without} some guest sitting on it). No two bowls locate at the same position.

As a waiter, you are supposed to assign each person with exactly one bowl of Uyghur Polo. The table can be rotated, so each time you can turn it 2πn\frac{2\pi}{n} degrees clockwise or counterclockwise. The bowls turn with the table while the chairs and guests do not move. When one bowl of Uyghur Polo is in front of a guest, he can either take it or wait for another.

You want to minimize the total times of table rotating so that everybody can have meals as quickly as possible.

(Formal definition: The boundary of the table is a circle. nn chairs are at nn points on the circle whose convex hull is a regular polygon with nn vertices. We name the points 0,,n10,\ldots, n-1 in counterclockwise order. The ii-th bowl is at point bib_i (0bi<n0\le b_i<n) initially. The ii-th guest is at point aia_i (0ai<n0\le a_i < n) initially. If you turn the table counterclockwise, the bowl at point bib_i (1ik1\le i\le k) will be moved to point (bi+1)modn(b_i+ 1) \bmod n after the rotation. If you turn the table clockwise, the bowl at point bib_i (1ik1\le i\le k) will be moved to point (bi1)modn(b_i-1) \bmod n after the rotation. (xmodnx\bmod n is defined as the smallest nonnegative integer rr such that xrx-r is a multiple of nn.))

输入格式

There are multiple test cases. The first line of the input contains an integer TT, indicating the number of test cases. For each test case:

The first line contains two integers n,kn,k (1n109,1kmin(n,1000)1\le n \le 10^9,1 \le k \le \min(n,1000)) indicating the size of the table and the number of persons and bowls of Uyghur Polo.

In the second line, there are kk integers a1,a2,,aka_1,a_2,\dots,a_k (0ai<n0 \le a_i < n), indicating the positions of the persons. No two guests share the same position.

In the third line, there are kk integers b1,b2,,bkb_1,b_2,\dots,b_k (0bi<n0 \le b_i < n), indicating the initial positions of the bowls. No two bowls of Uyghur Polo locate at the same position.

It is guaranteed that the sum of kk over all test cases does not exceed 50005000.

输出格式

For each test case, output the minimal total times of rotations such that each guest can have exactly one bowl of Uyghur Polo.

题目大意

Wowo 是一名好客的新疆大叔。kk 名客人将在 Wowo 的房子里,围在圆桌旁享用新疆抓饭。n(nk)n(n \ge k) 把椅子已经被均匀的放置在桌子周围,每个客人坐在一把椅子上,没有两名客人坐在同一把椅子上。kk 份新疆抓饭已经放在了桌子上,每份抓饭都放在了一把椅子面前(可能没有客人坐在这把椅子上),没有两份抓饭放在同一个位置上。

作为服务员,你需要为每一名客人指定正好一碗抓饭。桌子可以旋转,每次你可以顺时针或逆时针旋转桌子 2πn\dfrac{2\pi}{n} 度。碗随着桌子旋转,但客人和椅子不会随之移动。当一碗饭转到某个客人面前时,他可以选择拿走这碗饭,或等待另一碗。

你想要最小化旋转桌子的次数,使得每个人可以最快得到饭。

1
4 2
0 3
1 2
2
1
14 5
0 12 13 8 9
9 2 6 13 5
6