QOJ.ac

QOJ

時間限制: 2 s 記憶體限制: 1024 MB 總分: 100

#3896. 不完整的实现

统计

归并排序是一种排序算法。它通过将数组对半拆分,递归地对两半进行排序,然后将这两半合并,从而实现对整个数组的排序。你的朋友正在尝试实现归并排序算法,但不幸的是,他还没完全搞定:他只能对数组的一半进行排序!他非常绝望,于是向你求助:你能利用他未完成的代码编写一个能完全排序数组的算法吗?

目前,你朋友的代码是一个排序函数,它可以作用于任意子数组,只要该子数组的长度恰好是原数组长度的一半。它随后会正确地对该子数组进行排序。注意,子数组不必是连续的,它可以是原数组的任意子集!

你决定试用这个函数。你从一个乱序数组开始,尝试对其进行排序(见图 I.1)。在选择了 3 个子数组并将其作为排序函数的输入后,你最终得到了一个有序数组。有趣的是,无论原数组是什么,你似乎总能通过调用你朋友的排序函数 3 次来将其完全排序。你决定将此作为一个挑战:你想要扩展该代码以处理完整数组,且最多调用 3 次排序函数。

现在你需要找出要排序的子数组!给定一个长度为 $n$ 的数组,输出最多 3 个长度为 $\frac{1}{2}n$ 的子数组,使得按顺序对这些子数组进行排序后,原数组将变为有序。保证这总是可行的。

图 I.1:样例输出 1 的第一次排序步骤

输入格式

输入包含: 一行包含一个整数 $n$ ($4 \le n \le 10^5$),且 $n$ 能被 4 整除,表示数组的长度。 一行包含 $n$ 个互不相同的整数 $a_i$ ($1 \le a_i \le n$),表示待排序的数组。

输出格式

输出包含: 一行包含函数调用次数 $f$ ($0 \le f \le 3$)。 $f$ 行,每行包含 $\frac{1}{2}n$ 个互不相同的整数 $b_i$ ($1 \le b_i \le n$),表示每次函数调用时待排序子数组的下标。

如果存在多个有效解,你可以输出其中任意一个。你不需要最小化 $f$。

样例

样例输入 1

8
3 8 4 7 1 5 2 6

样例输出 1

3
2 3 6 8
1 3 4 5
2 4 5 7

样例输入 2

4
1 4 3 2

样例输出 2

3
3 4
2 3
3 4

样例输入 3

8
1 4 8 7 5 6 3 2

样例输出 3

2
6 5 3 8
4 3 7 2

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.