#P6549. [COCI 2010/2011 #2] KNJIGE

[COCI 2010/2011 #2] KNJIGE

Description

Mirko has a home library consisting of nn books. The nn books are arranged one after another in a narrow cabinet. Since he practiced letters well in the previous task, he now wants to arrange the books in alphabetical order, so that the book whose title is the smallest in lexicographical order is at the top, and the book whose title is the largest in lexicographical order is at the bottom.

Mirko can easily pull a book out of the cabinet, but it is hard to push it back in. Therefore, he can only put a book back onto the top of the cabinet. As a result, the only available way to sort the books is to repeatedly pull a book out of the cabinet and place it on the top.

The books are labeled with integers from 11 to nn in alphabetical order. Therefore, Mirko wants to sort them from top to bottom into (1,2,,n)(1,2,\cdots,n). For example, if n=3n = 3 and the initial order is (3,2,1)(3,2,1), then two moves are enough. First, he pulls out the book numbered 22 and puts it on the top, so the order becomes (2,3,1)(2,3,1). After that, he does the same with the book numbered 11, so the order becomes (1,2,3)(1,2,3).

Given the initial order, compute the minimum number of moves needed to finish sorting.

Input Format

The first line contains an integer nn, as described in the statement.

The next nn lines each contain one positive integer, describing the initial order of the books.

Output Format

Output one integer on a single line, representing the minimum number of moves required to arrange the books in increasing order.

3
3
2
1
2
4
1
3
4
2
2

Hint

Constraints

For 100%100\% of the data, it is guaranteed that 1n3×1051 \leq n \leq 3 \times 10^5, and the initial order of the books is a permutation of 1n1 \ldots n.

Notes

  • This problem is worth 8080 points.

  • Translated from COCI2010-2011 CONTEST #2 KNJIGE. Translator:

    https://www.luogu.com.cn/user/115711

Translated by ChatGPT 5