Giraffes

아직 제출이 없습니다시간 제한7초메모리 제한1024 MB

문제

IOI Zoo is famous for giraffes. There are NN giraffes in IOI Zoo, numbered from 11 to NN in ascending order of their heights. The heights of the giraffes are different from each other. There are NN cages placed in a straight line, numbered from 11 to NN from left to right. In each cage, a giraffe is living in it. The giraffe P_iP\_i is living in the cage ii.

Mr. APIO is the curator of IOI Zoo. He is worrying about the ratings of IOI Zoo in reviews. IOI Zoo receives low ratings for the reason that “the visual quality of the giraffes is bad.” Precisely, when a visitor takes a picture of giraffes, the visitor chooses integers ll, rr (1lrN1 ≤ l ≤ r ≤ N) and takes a picture of the giraffes in the cages l,l+1,,rl, l + 1, \dots ,r. Then, the visual quality of the giraffes becomes bad if both of the following conditions are satisfied.

  • There is a giraffe in the picture which is taller than the giraffes in the both ends of the picture. In other words, there is an integer kk (l<k<rl < k < r) satisfying P_l<P_k>P_rP\_l < P\_k > P\_r.
  • There is a giraffe in the picture which is shorter than the giraffes in the both ends of the picture. In other words, there is an integer kk (l<k<rl < k < r) satisfying P_l>P_k<P_rP\_l > P\_k < P\_r.

Mr. APIO will arrange the positions of the giraffes so that the visual quality of the giraffes does not become bad for any choice of ll, rr (1lrN1 ≤ l ≤ r ≤ N) when a visitor takes a picture. Since it takes a lot of work to move a giraffe from one cage to another cage, he wants to minimize the number of giraffes which are moved. Of course, after he finishes moving the giraffes, in each cage, a giraffe should be living in it.

Given information of the current positions of the giraffes, write a program which calculates the minimum number of giraffes which are moved. Since Mr. APIO arranged the current positions of the giraffes at random, you may assume that the values P_iP\_i (1iN1 ≤ i ≤ N) are generated at random (See Generation of Input Data for details).

입력

Read the following data from the standard input. Given values are all integers.

NN

P_1P_2P_NP\_1 P\_2 \cdots P\_N

출력

Write one line to the standard output. The output should contain the minimum number of giraffes which are moved.

제한

  • 1N8,0001 ≤ N ≤ 8\\,000.
  • 1P_iN1 ≤ P\_i ≤ N (1iN1 ≤ i ≤ N).
  • P_iP_jP\_i ≠ P\_j (1i<jN1 ≤ i < j ≤ N).
  • The values P_iP\_i (1iN1 ≤ i ≤ N) are generated at random (See Generation of Input Data for details).

힌트

Generation of Input Data

In this task, except for Sample Input, there are 10 test cases which satisfy the constraints of Subtasks 1, 2, 3, 4. Also, there are 10 test cases which satisfy the constraints of Subtasks 2, 3, 4 only, there are 10 test cases which satisfy the constraints of Subtasks 3, 4 only, and there are 10 test cases which satisfy the constraints of Subtask 4 only. In total, including Sample Input, 44 test cases are used for grading. All the 44 test cases for this task are generated by the following way.

  1. First, choose NN satisfying the constraints of each subtask.
  2. Then, among the N!=1×2××NN! = 1 × 2 × \cdots × N permutations (P_1,P_2,,P_N)(P\_1, P\_2, \dots , P\_N) satisfying the constraints, choose one permutation at random with equal probability and generate P_1,P_2,,P_NP\_1, P\_2, \dots , P\_N.