#P8425. [JOI Open 2022] 长颈鹿 / Giraffes

[JOI Open 2022] 长颈鹿 / Giraffes

Background

Translated from JOI Open 2022 T2. キリン / Giraffes.

Problem Description

The IOI Zoo is famous for giraffes. There are NN giraffes in the IOI Zoo, numbered 11 to NN in increasing order of height. All giraffes have distinct heights. There are NN cages arranged in a row, numbered 11 to NN from left to right. Each cage contains exactly one giraffe. Cage ii contains giraffe PiP_i.

Mr. APIO is the director of the IOI Zoo. He is worried about the zoo’s rating. The IOI Zoo has received bad reviews because “the giraffes look bad”. More precisely, when a visitor takes a photo of giraffes, the visitor chooses two integers l,rl, r (1lrN1 \le l \le r \le N) and takes a photo of the giraffes in cages l,l+1,,rl, l + 1, \ldots, r. Then, the giraffes in the photo look bad if both of the following conditions are satisfied.

  • There exists a giraffe that is taller than both end giraffes. In other words, there exists an integer kk (l<k<rl < k < r) such that Pl<Pk>PrP_l < P_k > P_r.
  • There exists a giraffe that is shorter than both end giraffes. In other words, there exists an integer kk (l<k<rl < k < r) such that Pl>Pk<PrP_l > P_k < P_r.

Mr. APIO will rearrange the giraffes so that for any choice of l,rl, r (1lrN1 \le l \le r \le N) by a visitor, the giraffes in the photo will never look bad. Since moving a giraffe from one cage to another is troublesome, he wants to minimize the number of giraffes that are moved. Of course, after moving, each cage must still contain exactly one giraffe.

Given the current information about the giraffes, write a program to compute the minimum number of giraffes that need to be moved. Since Mr. APIO’s current arrangement is random, you may assume that the values of PiP_i (1iN1 \le i \le N) are generated randomly (see the Testdata Generation section for details).

Input Format

The first line contains a positive integer NN.

The second line contains NN positive integers P1,P2,,PNP_1, P_2, \ldots, P_N.

Output Format

Output one line containing one integer, the minimum number of giraffes that need to be moved.

6
5 4 6 1 3 2

2

4
4 1 3 2

0

7
3 1 6 7 4 2 5

2

13
8 5 6 13 4 2 11 3 9 1 10 7 12

6

Hint

Sample Explanation #1.

There are 66 giraffes in the IOI Zoo. The giraffes 5,4,6,1,3,25, 4, 6, 1, 3, 2 live in cages from left to right in that order. With this arrangement, if a visitor takes a photo with l=2,r=5l = 2, r = 5, then the giraffes look bad. The two conditions are satisfied as follows.

  • The giraffe in cage 33 is taller than the giraffes in the leftmost cage (cage 22) and the rightmost cage (cage 55).
  • The giraffe in cage 44 is shorter than the giraffes in the leftmost cage (cage 22) and the rightmost cage (cage 55).

If Mr. APIO moves giraffe 11 from cage 44 to cage 11, and then moves giraffe 55 from cage 11 to cage 44, then no matter what the visitor chooses, the giraffes will not look bad. Mr. APIO achieves the goal by moving 22 giraffes. Since this is the minimum possible number of moved giraffes, the output is 22.

This sample satisfies the constraints of all subtasks.


Sample Explanation #2.

There are 44 giraffes in the IOI Zoo. The giraffes 4,1,3,24, 1, 3, 2 live in cages from left to right in that order. With this arrangement, for any choice by the visitor, the giraffes will not look bad. Mr. APIO does not need to move any giraffes, so the output is 00.

This sample satisfies the constraints of all subtasks.


Sample Explanation #3.

For example, suppose Mr. APIO arranges the giraffes 3,5,6,7,4,2,13, 5, 6, 7, 4, 2, 1 in cages from left to right in that order. For any choice by the visitor, the giraffes will not look bad. Mr. APIO achieves the goal by moving 22 giraffes. Since this is the minimum possible number of moved giraffes, the output is 22.

This sample satisfies the constraints of all subtasks.


Sample Explanation #4.

This sample satisfies the constraints of subtasks 2, 3, and 4.


Constraints

This problem uses bundled testdata.

  • Subtask 1 (10 points): N7N \le 7.
  • Subtask 2 (22 points): N13N \le 13.
  • Subtask 3 (27 points): N300N \le 300.
  • Subtask 4 (41 points): no additional constraints.

For all testdata, 1N80001 \le N \le 8000, 1PiN1 \le P_i \le N, all PiP_i are distinct, and it is guaranteed that PiP_i is generated randomly (see the Testdata Generation section for details).


Testdata Generation

In this problem, besides the sample inputs, there are 1010 test cases satisfying the constraints of subtasks 1, 2, 3, and 4; 1010 test cases satisfying the constraints of subtasks 2, 3, and 4; 1010 test cases satisfying the constraints of subtasks 3 and 4; and 1010 test cases satisfying the constraints of subtask 4. Including the samples, there are 4444 test cases in total for scoring. All 4444 test cases are generated as follows:

  1. First, generate NN that satisfies the subtasks.
  2. Then, among the N!=1×2××NN! = 1 \times 2 \times \cdots \times N permutations (P1,P2,,PN)(P_1, P_2, \ldots, P_N) that satisfy the constraints, choose one uniformly at random as P1,P2,,PNP_1, P_2, \ldots, P_N.

Translated by ChatGPT 5