QOJ.ac

QOJ

حد الوقت: 3.0 s حد الذاكرة: 256 MB مجموع النقاط: 100 قابلة للهجوم ✓

#14093. Replace Sort

الإحصائيات

考虑一个数组 $A$ 和一个整数集合 $B$,满足 $A$ 和 $B$ 中的所有数字互不相同。你的任务是将 $A$ 转换为一个升序排序的数组。为此,你可以从 $B$ 中取出任意数字并替换 $A$ 中的任意元素。你可以执行此操作任意次,但 $B$ 中的每个元素最多只能使用一次。

确定将 $A$ 转换为排序数组所需的最少操作次数,或者确定这是不可能的。

输入格式

第一行包含两个整数 $N$ 和 $M$ ($1 \le N, M \le 5 \cdot 10^5$) —— 分别表示 $A$ 和 $B$ 的大小。

第二行包含 $N$ 个整数 $A_1, A_2, \dots, A_N$。

第三行包含 $M$ 个整数 $B_1, B_2, \dots, B_M$。

所有 $(N + M)$ 个元素互不相同、均为正数且不超过 $10^9$。

输出格式

如果无法将 $A$ 转换为排序数组,输出 $-1$。否则,输出所需的最少操作次数。

样例

输入样例 1

4 1
2 6 13 10
5

输出样例 1

-1

输入样例 2

4 2
2 6 13 10
5 4

输出样例 2

2

输入样例 3

4 3
2 6 13 10
5 4 19

输出样例 3

1

说明

在所有三个样例中,问题都在于 $13 > 10$,因此我们必须至少修改其中一个。

在第一个样例中,我们可以通过将 $13$ 替换为 $5$ 来减小它,但这会破坏另一侧的单调性,因此无解。

在第二个样例中,我们还有 $4$,可以用它来修复被破坏的一侧。无法用少于 $2$ 次操作完成。

在第三个样例中,我们最终可以增加最后一个元素,从而通过 $1$ 次操作修复 $A$。

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.