QOJ.ac

QOJ

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

#6116. 修改序列

統計

给定两个长度均为 $N$ 的序列 $A = (a_1, \dots, a_N)$ 和 $B = (b_1, \dots, b_N)$。其中 $a_i$ 和 $b_j$ 均为 $1$ 到 $M$ 之间的整数。

你可以对序列 $A$ 进行一次魔法操作:准备一个 $1$ 到 $M$ 的排列 $P = (p_1, \dots, p_M)$,并将序列 $A$ 变为 $A'$,其中 $a'_i = p_{a_i}$ ($1 \le i \le N$)。

你希望通过魔法操作将 $A$ 变为 $A'$,使得 $A'$ 与序列 $B$ 之间的距离尽可能小。两个序列之间的距离定义为汉明距离,即两个等长序列中对应位置数值不同的位置数量。

在所有可能的 $A'$ 中,你需要找到一个满足以下条件的序列:

  • 不存在其他可能的 $A'$ 使得其与 $B$ 的距离小于该序列与 $B$ 的距离。
  • 在所有与 $B$ 距离相同的可能序列中,该序列是字典序最小的。

序列 $X = (x_1, x_2, \dots, x_N)$ 比另一个等长序列 $Y = (y_1, y_2, \dots, y_N)$ “字典序更小”,当且仅当存在一个下标 $i$ ($1 \le i \le N$),使得对于所有 $j$ ($1 \le j < i$) 都有 $x_j = y_j$,且 $x_i < y_i$。

输入格式

第一行包含两个整数 $N$ ($1 \le N \le 100\,000$) 和 $M$ ($1 \le M \le 60$),分别表示序列的长度以及序列中数值的范围。

第二行包含 $N$ 个整数,第 $i$ 个整数记为 $a_i$ ($1 \le a_i \le M$)。

第三行包含 $N$ 个整数,第 $i$ 个整数记为 $b_i$ ($1 \le b_i \le M$)。

输出格式

输出 $N$ 个整数,中间用空格隔开。第 $i$ 个整数应为满足题目所有条件的序列的第 $i$ 个元素。每个元素应以整数形式输出。

样例

样例输入 1

4 3
2 2 3 3
2 2 2 2

样例输出 1

1 1 2 2

样例输入 2

5 3
2 2 3 3 2
2 2 2 2 3

样例输出 2

3 3 2 2 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.