#P9771. [HUSTFC 2023] 排列排序问题

[HUSTFC 2023] 排列排序问题

Problem Description

JokerShaco has a permutation pp of length nn. He believes a permutation must be ordered, so he plans to sort it.

He can perform the following operations on this permutation:

  • Cut the permutation into several sequences (or do not cut it and keep it as is). Each sequence must contain at least one element.
  • Choose some of these sequences and reverse them.
  • Recombine and concatenate these sequences in any order he wants to obtain a new permutation.

JokerShaco thinks the cutting operation is very tiring. He wants to know: if the permutation must be made ordered, what is the minimum number of cuts required.

A permutation of length nn is defined as a sequence containing nn distinct integers from 11 to nn, where each integer appears exactly once.

The definition of reversing a sequence is: suppose there is a sequence of length mm, [a1,a2,,am1,am][a_1,a_2,\dots,a_{m-1},a_m]. After reversing it, it becomes [am,am1,,a2,a1][a_m,a_{m-1},\dots,a_2,a_1].

Input Format

The first line contains an integer n (1n106)n\ (1\le n\le 10^6), representing the length of the permutation pp.

The second line contains nn integers, where the ii-th integer is defined as pip_i. It is guaranteed that the input pp is a permutation of length nn.

Output Format

Output one integer, representing the minimum number of cuts needed to make the permutation pp ordered.

5
1 2 3 5 4

1

3
3 2 1

0

Hint

Translated by ChatGPT 5