#C10840. Minimum Moves to Sort the Deck

    ID: 40090 Type: Default 1000ms 256MiB

Minimum Moves to Sort the Deck

Minimum Moves to Sort the Deck

You are given a deck of n uniquely numbered cards from 1 to n arranged in an arbitrary order. In one move, you can remove any card from the deck and insert it at any position. The goal is to sort the deck in ascending order (i.e. 1, 2, 3, …, n).

The key observation is that if you can keep a longest increasing subsequence (with respect to the natural order of card numbers) intact, then you only need to reposition the remaining cards.

Formally, if L is the length of the longest subsequence of cards for which the positions are in increasing order when the cards are scanned in numerical order, then the minimum number of moves required is \[ n - L \] where n is the total number of cards.

Your task is to compute this minimum number of moves.

inputFormat

The input is read from stdin and consists of two lines:

  • The first line contains a single integer n (1 ≤ n ≤ 105), representing the number of cards.
  • The second line contains n space-separated integers, which represent the current order of the cards in the deck.

outputFormat

Output a single integer to stdout: the minimum number of moves required to sort the deck in ascending order.

## sample
5
4 3 2 5 1
3

</p>