QOJ.ac

QOJ

Time Limit: 3.0 s Memory Limit: 256 MB Total points: 100 Hackable ✓

#14093. 替換排序

Statistics

給定一個整數陣列 $A$ 和一個整數集合 $B$,其中 $A$ 與 $B$ 中的所有數都互不相同。你的任務是將 $A$ 變成已排序的陣列。為此,你可以從 $B$ 中取出任意一個數,用它替換 $A$ 中的任意一個元素。你可以進行任意多次這種操作,但 $B$ 中的每個元素至多只能使用一次。

求出將 $A$ 變成已排序陣列所需的最少操作次數,或判定不可能做到。

輸入格式

輸入的第一行包含兩個整數 $N$ 和 $M$($1 \le N,M \le 5\cdot10^5$),分別表示 $A$ 和 $B$ 的大小。

第二行包含 $N$ 個整數 $A_1,A_2,\ldots,A_N$。

第三行包含 $M$ 個整數 $B_1,B_2,\ldots,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.