Derangement

AtCoder
IDarc082_b
Time2000ms
Memory256MB
Difficulty
You are given a permutation $p_1,p_2,...,p_N$ consisting of $1$,$2$,..,$N$. You can perform the following operation any number of times (possibly zero): Operation: Swap two **adjacent** elements in the permutation. You want to have $p_i ≠ i$ for all $1≤i≤N$. Find the minimum required number of operations to achieve this. ## Constraints * $2≤N≤10^5$ * $p_1,p_2,..,p_N$ is a permutation of $1,2,..,N$. ## Input The input is given from Standard Input in the following format: $N$ $p_1$ $p_2$ .. $p_N$ [samples]
Samples
Input #1
5
1 4 3 5 2
Output #1
2

Swap $1$ and $4$, then swap $1$ and $3$. $p$ is now $4,3,1,5,2$ and satisfies the condition. This is the minimum possible number, so the answer is $2$.
Input #2
2
1 2
Output #2
1

Swapping $1$ and $2$ satisfies the condition.
Input #3
2
2 1
Output #3
0

The condition is already satisfied initially.
Input #4
9
1 2 4 9 5 8 7 3 6
Output #4
3
API Response (JSON)
{
  "problem": {
    "name": "Derangement",
    "description": {
      "content": "You are given a permutation $p_1,p_2,...,p_N$ consisting of $1$,$2$,..,$N$. You can perform the following operation any number of times (possibly zero): Operation: Swap two **adjacent** elements in th",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "arc082_b"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a permutation $p_1,p_2,...,p_N$ consisting of $1$,$2$,..,$N$. You can perform the following operation any number of times (possibly zero):\nOperation: Swap two **adjacent** elements in th...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments