AT_arc234_a [ARC234A] Reverse, Recurse
Description
You are given a permutation $ P=(P_1,P_2,\ldots,P_N) $ of $ (1,2,\cdots,N) $ . For integers $ L,R $ satisfying $ 1\leq L\leq R\leq N $ , define the procedure $ f(L,R) $ as performing the following two steps in order.
- If $ P_L > P_R $ , reverse $ P_L, P_{L+1}, \cdots,P_R $ . That is, replace them with $ P_R, P_{R-1},\cdots, P_L $ .
- If $ L
Input Format
The input is given from Standard Input in the following format:
> $ N $ $ P_1 $ $ P_2 $ $ \ldots $ $ P_N $
Output Format
Output the answer.
Explanation/Hint
### Sample Explanation 1
Initially, $ P=(3,1,2) $ . The procedures are called in the following order.
- $ f(1,3) $ : Since $ P_1>P_3 $ , a reversal is performed, resulting in $ P=(2,1,3) $ .
- $ f(1,2) $ : Since $ P_1>P_2 $ , a reversal is performed, resulting in $ P=(1,2,3) $ .
- $ f(1,1) $ : Since $ P_1>P_1 $ does not hold, no reversal is performed.
- $ f(2,2) $ : Since $ P_2>P_2 $ does not hold, no reversal is performed.
- $ f(2,3) $ : Since $ P_2>P_3 $ does not hold, no reversal is performed.
- $ f(2,2) $ : Since $ P_2>P_2 $ does not hold, no reversal is performed.
- $ f(3,3) $ : Since $ P_3>P_3 $ does not hold, no reversal is performed.
A total of $ 2 $ reversals are performed, so the answer is $ 2 $ .
### Constraints
- $ 2 \le N \le 3 \times 10^5 $
- $ 1 \le P_i \le N $
- $ P $ is a permutation of $ (1,2,\cdots,N) $ .
- All input values are integers.