QOJ.ac

QOJ

実行時間制限: 2 s - 3 s メモリ制限: 1024 MB 満点: 50

#5799. 贿赂囚犯

統計

在一个王国里,有 $P$ 个排成一行的牢房(编号为 1 到 $P$)。牢房 $i$ 和 $i+1$ 相邻,相邻牢房中的囚犯被称为“邻居”。相邻的牢房之间有一扇带窗户的墙,邻居们可以通过这扇窗户交流。

所有的囚犯原本相安无事,直到一名囚犯被释放。当这种情况发生时,被释放囚犯的邻居会得知消息,并将其传达给他们的另一位邻居。该囚犯再将其传达给他的另一位邻居,以此类推,直到传达到没有其他邻居的囚犯为止(因为他身处 1 号牢房、$P$ 号牢房,或者另一侧的相邻牢房是空的)。如果一个囚犯得知有其他囚犯被释放,他就会愤怒地砸毁牢房里的一切,除非用一枚金币贿赂他。因此,在释放 $A$ 号牢房的囚犯后,所有位于 $A$ 号牢房两侧——直到 1 号牢房、$P$ 号牢房或空牢房为止——的囚犯都需要被贿赂。

假设每个牢房最初恰好住着一名囚犯,且每天只能释放一名囚犯。给定 $Q$ 天内要释放的 $Q$ 名囚犯的列表,请找出如果可以按任意顺序释放囚犯,所需贿赂金币总数的最小值。

注意,每次贿赂仅在当天有效。如果一名昨天被贿赂过的囚犯今天又听到了有其他囚犯被释放的消息,那么他需要再次被贿赂。

输入格式

输入的第一行包含测试用例的数量 $N$。接下来是 $N$ 个测试用例。每个测试用例包含 2 行。 第一行格式为:

P Q

其中 $P$ 是牢房数量,$Q$ 是要释放的囚犯数量。

随后的一行包含 $Q$ 个不同的牢房编号(即要释放的囚犯所在位置),以空格分隔,并按升序排列。

输出格式

对于每个测试用例,输出一行,格式为:

Case #X: C

其中 $X$ 是测试用例编号(从 1 开始),$C$ 是所需贿赂金币的最小值。

数据范围

$1 \le N \le 100$ $Q \le P$ 每个牢房编号在 $1$ 到 $P$ 之间(含边界)。

样例

样例输入 1

2
8 1
3
20 3
3 6 14

样例输出 1

Case #1: 7
Case #2: 35

说明

在第二个样例中,如果先释放 14 号牢房的囚犯,然后是 6 号,最后是 3 号,所需金币总数为 $19 + 12 + 4 = 35$。如果先释放 6 号牢房的囚犯,成本则为 $19 + 4 + 13 = 36$。

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.