201243: [AtCoder]ARC124 D - Yet Another Sorting Problem

Memory Limit:1024 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $700$ points

Problem Statement

Given is a sequence $p$ of length $N+M$, which is a permutation of $(1,2 \ldots, N+M)$. The $i$-th term of $p$ is $p_i$.

You can do the following Operation any number of times.

Operation: Choose an integer $n$ between $1$ and $N$ (inclusive), and an integer $m$ between $1$ and $M$ (inclusive). Then, swap $p_{n}$ and $p_{N+m}$.

Find the minimum number of Operations needed to sort $p$ in ascending order. We can prove that it is possible to sort $p$ in ascending order under the Constraints of this problem.

Constraints

  • All values in input are integers.
  • $1 \leq N,M \leq 10^5$
  • $1 \leq p_i \leq N+M$
  • $p$ is a permutation of $(1,2 \ldots, N+M)$.

Input

Input is given from Standard Input in the following format:

$N$ $M$
$p_{1}$ $\cdots$ $p_{N+M}$

Output

Print the minimum number of Operations needed to sort $p$ in ascending order.


Sample Input 1

2 3
1 4 2 5 3

Sample Output 1

3

Sample Input 2

5 7
9 7 12 6 1 11 2 10 3 8 4 5

Sample Output 2

10

Input

题意翻译

给定长度为 $n+m$ 的排列 $p$,其中 $1$ 至 $n$ 位置为白色,$n+1$ 至 $n+m$ 位置为黑色,每次操作定义为交换一个白色位置与一个黑色位置的数,求把 $p$ 变成升序的最少操作次数。 $n,m \leq 10^5$ 第一行输入 $n,m$,第二行输入 $p_i$,保证其是 $n+m$ 的排列;输出一行一个整数,代表最少操作数。

加入题单

算法标签: