Yet Another Sorting Problem

AtCoder
IDarc124_d
Time2000ms
Memory256MB
Difficulty
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}$ [samples]
Samples
Input #1
2 3
1 4 2 5 3
Output #1
3
Input #2
5 7
9 7 12 6 1 11 2 10 3 8 4 5
Output #2
10
API Response (JSON)
{
  "problem": {
    "name": "Yet Another Sorting Problem",
    "description": {
      "content": "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 ",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "arc124_d"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "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$.\nYou can do the following **Operation** any number of times.\nOperation: Choose ...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments