#P7406. [JOI 2021 Final] 集体照 / Group Photo

[JOI 2021 Final] 集体照 / Group Photo

Description

There are NN people, numbered from 11 to NN. The height of person hh is hh.

There are NN steps, numbered from 11 to NN from lowest to highest. Step ii is lower than step i+1i+1 by 22 units of height. Each step can hold at most one person. Person HiH_i stands on step ii.

You may perform the following operation any number of times:

  • Choose i[1,N1]i \in [1, N-1] and swap the person on step ii with the person on step i+1i+1.

Suppose the height of the person standing on step ii is aia_i. You must make the arrangement satisfy:

  • For every i[1,N1]i \in [1, N-1], ai<ai+1+2a_i < a_{i+1} + 2.

Find the minimum number of operations.

Input Format

The first line contains an integer NN, the number of people.

The second line contains NN integers HiH_i, meaning that person HiH_i stands on step ii.

Output Format

Print one integer, the minimum number of operations.

5
3 5 2 4 1
3
5
3 2 1 5 4
0
9
6 1 3 4 9 5 7 8 2
9

Hint

Explanation for Sample 1

Let hih_i be the height of the person standing on step ii:

  • Swap person 22 and person 33, hi={3,2,5,4,1}h_i = \{3, 2, 5, 4, 1\}.
  • Swap person 44 and person 55, hi={3,2,5,1,4}h_i = \{3, 2, 5, 1, 4\}.
  • Swap person 33 and person 44, hi={3,2,1,5,4}h_i = \{3, 2, 1, 5, 4\}.

After exactly 33 steps, the condition is satisfied.

Explanation for Sample 2

The condition is already satisfied, so no operations are needed.

Constraints

This problem uses bundled testdata.

  • Subtask 1 (5 pts): N9N \le 9.
  • Subtask 2 (7 pts): N20N \le 20.
  • Subtask 3 (32 pts): N200N \le 200.
  • Subtask 4 (20 pts): N800N \le 800.
  • Subtask 5 (36 pts): no additional constraints.

For all testdata, 3N50003 \le N \le 5000, 1HiN1 \le H_i \le N, and all HiH_i are distinct.

Note

Translated from The 20th Japanese Olympiad in Informatics Final Round C 集合写真 English version: Group Photo

Translated by ChatGPT 5