QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 128 MB 總分: 10

#10388. 度假 [A]

统计

Byteasar 准备去 Byteland 度假。现在他正在规划行程,并试图找出他最想参观的旅游景点。他找到了几本关于 Byteland 的在线指南,每本指南都包含了一份所有景点的排名。现在,Byteasar 想要创建属于他自己的排名。

景点编号为 $1$ 到 $n$。每份排名都是一个包含 $1$ 到 $n$ 的序列,按推荐程度从高到低排列。两份排名之间的距离计算如下:对于每个景点,我们找到它在两份序列中出现的位置 $p_1$ 和 $p_2$。我们计算 $\min(|p_1 - p_2|, 8)$,该值代表了两个排名在该景点上的差异程度。所有景点的该值之和即为两份排名之间的距离。

Byteasar 希望创建一个排名,使得该排名与他找到的所有在线排名之间的距离之和尽可能小。

输入格式

输入的第一行包含两个整数 $n$ 和 $k$ ($2 \le n \le 5\,000$, $2 \le k \le 3$),分别表示 Byteland 的景点数量和 Byteasar 找到的在线指南数量。接下来的 $k$ 行,每行描述一份在线排名。每份排名由 $n$ 个 $1$ 到 $n$ 之间的整数组成,其中每个数字恰好出现一次。景点按推荐程度从高到低排列。

输出格式

输出的第一行且仅包含一行一个整数:Byteasar 的排名与他找到的在线排名之间的最小总距离。

样例

输入 1

5 2
5 2 4 3 1
2 4 1 3 5

输出 1

8

说明

Byteasar 可以创建的排名包括但不限于:2 4 5 3 15 2 4 3 1

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.