QOJ.ac

QOJ

Time Limit: 2.0 s Memory Limit: 1024 MB Total points: 100

#15466. CPEquivalence

统计

给定一个由整数组成的数组 $x$(即 $x$ 的每一项都是整数),最近位置数组(CP-array)$\text{CP}(x)$ 是一个长度为 $|x|$ 的数组,定义为: $$\text{CP}(x)[i] = \max(\{j \mid j < i; x[j] \ge x[i]\} \cup \{-1\}) \text{ 对于所有 } 0 \le i < |x|$$ 其中 $x[i]$ 表示 $x$ 中的第 $i$ 个整数,$|x|$ 表示 $x$ 中整数的个数。换句话说,$\text{CP}(x)[i]$ 是 $x$ 中小于 $i$ 且对应项大于或等于 $x[i]$ 的最大索引。例如,当 $x = [64, 2, 5, 100, 100]$ 时,其 CP 数组为 $\text{CP}(x) = [-1, 0, 0, -1, 3]$,且 $|x| = 5$。

如果两个整数数组 $x$ 和 $y$ 满足 $\text{CP}(x) = \text{CP}(y)$,则称它们是 CP 等价的。显然,两个 CP 等价的整数数组 $x$ 和 $y$ 具有相同的长度。例如,数组 $x = [64, 2, 5, 100, 100]$ 和 $y = [3, 1, 2, 4, 1]$ 是 CP 等价的,因为它们的 CP 数组均为 $[-1, 0, 0, -1, 3]$。

对于整数数组 $x$、整数 $a$ 和非负整数 $i < |x|$,在 $x$ 的位置 $i$ 处执行替换操作得到数组 $[x[0], x[1], \dots, x[i-1], a, x[i+1], \dots, x[|x|-1]]$。对于整数数组 $x$ 和非负整数 $i < |x|$,在 $x$ 的位置 $i$ 处执行删除操作得到数组 $[x[0], x[1], \dots, x[i-1], x[i+1], \dots, x[|x|-1]]$。最后,对于整数数组 $x$、整数 $a$ 和非负整数 $i \le |x|$,在 $x$ 的位置 $i$ 处执行插入操作得到数组 $[x[0], x[1], \dots, x[i-1], a, x[i], \dots, x[|x|-1]]$。编辑操作是指在单个位置上进行的插入、删除或替换操作之一。

给定两个整数数组 $x$ 和 $y$,计算将 $y$ 转换为满足 $\text{CP}(x) = \text{CP}(y')$ 的数组 $y'$ 所需的最少编辑操作次数。

例如,设 $x = [64, 2, 5, 100, 100]$,$y = [-5, -5, -5, -5]$。考虑数组 $y' = [-5, -6, -5, -4, -5]$。此时,$\text{CP}(y') = [-1, 0, 0, -1, 3]$,因此 $x$ 和 $y'$ 是 CP 等价的。我们可以通过对 $y$ 执行两次编辑操作得到数组 $y'$,这是最少操作次数。

输入格式

程序从标准输入读取数据。输入包含三行。第一行包含两个整数 $n$ 和 $m$ ($1 \le n \le 40$; $1 \le m \le 40$),分别表示 $x$ 和 $y$ 的长度。第二行包含 $n$ 个介于 $-1,000,000$ 和 $1,000,000$(含)之间的整数,表示数组 $x$。第三行包含 $m$ 个介于 $-1,000,000$ 和 $1,000,000$(含)之间的整数,表示数组 $y$。

输出格式

程序向标准输出写入数据。输出仅一行,包含将 $y$ 转换为满足 $\text{CP}(x) = \text{CP}(y')$ 的整数数组 $y'$ 所需的最少编辑操作次数。

样例

样例输入 1

5 5
64 2 5 100 100
3 1 2 4 1

样例输出 1

0

样例输入 2

5 4
64 2 5 100 100
-5 -5 -5 -5

样例输出 2

2

样例输入 3

6 5
1 2 3 4 5 6
2 5 3 4 5

样例输出 3

3

样例输入 4

6 3
1 3 5 2 5 2
5 5 6

样例输出 4

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.