QOJ.ac

QOJ

Time Limit: 5.0 s Memory Limit: 256 MB Total points: 100

#6797. 不确定的捕获

Statistics

一些囚犯从监狱中逃脱了!你的任务是把他们抓回来。

在时间 $0$ 时,城市里有 $n$ 名囚犯,每名囚犯都在不同的位置。我们将这些位置标记为 $1$ 到 $n$。

你的同事制定了一个计划,尽可能多地封锁道路,使得每个位置都只有一条出路。每过一小时,位于位置 $i$ 的囚犯会离开并立即到达位置 $a_i$ ($1 \le a_i \le n$)。

你可以在任意整数时间在任意位置安排一次突袭。此时在同一位置的所有囚犯都将被逮捕。然而,你只能进行一次突袭,之后所有在逃的囚犯都会离开城市。

经过考虑,你决定联系你的同事修改他们的计划。具体来说,你可以在时间 $0$ 之前修改一些 $a_i$(修改后仍需满足 $1 \le a_i \le n$)。

现在你想知道,为了抓住所有囚犯,最少需要修改多少个 $a_i$(设答案为 $K$)。你还想知道,如果你最多可以修改 $a_i$ 的次数为 $j$ ($j \in \{0, 1, 2, \dots, K\}$),你最多能逮捕多少名囚犯。请编写一个程序来解决这个问题。

输入格式

输入包含多组测试数据。第一行包含一个整数 $T$ ($1 \le T \le 10^4$),表示测试数据的组数。对于每组测试数据:

第一行包含一个整数 $n$ ($1 \le n \le 10^5$),表示囚犯和位置的数量。

第二行包含 $n$ 个整数 $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$),其含义如上所述。

其中只有约 $5$ 组测试数据的 $n$ 大于 $50$。

输出格式

对于每组测试数据,输出两行。

第一行输出一个整数 $K$,表示为了抓住所有囚犯,最少需要修改的 $a_i$ 的数量。

第二行输出 $K+1$ 个由空格分隔的整数,分别表示如果你最多可以修改 $a_i$ 的次数为 $0, 1, 2, \dots, K$ 时,最多能逮捕的囚犯数量。

请不要在每行末尾打印多余的空格。

样例

样例输入 1

2
1
1
10
2 3 1 4 5 7 6 6 6 7

样例输出 1

0
1
3
3 6 9 10

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#327EditorialOpen题解jiangly2025-12-14 07:06:31View

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.