QOJ.ac

QOJ

Límite de tiempo: 4 s Límite de memoria: 2048 MB Puntuación total: 100

#2296. 交换生

Estadísticas

一群在雷克雅未克大学的交换生正在排队准备拍摄合影。从左到右,学生们的身高分别为 $g_1, g_2, \dots, g_n$。然而,摄影师希望重新排列这些学生,使得他们的身高顺序变为 $h_1, h_2, \dots, h_n$。为了做到这一点,摄影师将反复交换两名交换生。只有当两名交换生能够互相看见时,他们才能进行交换,即他们之间的每一名学生的身高都必须严格小于这两名要交换的学生的身高。

Silhouettes via Freepik

确定将学生排列成摄影师期望的顺序所需的最少交换次数,并找到一个该最小长度的交换序列。摄影师最多只有时间进行 $2 \cdot 10^5$ 次交换。如果需要更多的交换,你只需要确定前 $2 \cdot 10^5$ 次交换即可。

输入格式

输入包含: 一行一个整数 $n$ ($1 \le n \le 3 \cdot 10^5$),表示学生人数。 一行 $n$ 个整数 $g_1, g_2, \dots, g_n$ ($1 \le g_i \le 10^9$),表示学生的身高。 * 一行 $n$ 个整数 $h_1, h_2, \dots, h_n$ ($1 \le h_i \le 10^9$),表示摄影师期望的身高顺序。

保证 $(h_1, \dots, h_n)$ 是 $(g_1, \dots, g_n)$ 的一个排列。

输出格式

首先输出一个整数 $s$,表示所需的最少交换次数。然后输出 $\min(s, 2 \cdot 10^5)$ 次交换,每次交换包含两个整数 $i$ 和 $j$,表示该步骤中需要交换的学生的一对一位置(从 1 开始计数)。

如果存在多种有效的解决方案,你可以输出其中任意一种。

样例

输入格式 1

3
2 1 3
3 1 2

输出格式 1

1
1 3

输入格式 2

5
6 7 5 9 6
9 6 7 6 5

输出格式 2

4
3 4
4 5
1 2
1 3

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.