Cards in Place
InterviewTime limit1sMemory limit1024 MB
Given a row of cards with values, find the fewest cards to remove so the remaining cards read 1, 2, 3, ... from left to right.
- Level
Easy3 of 10
- Topics
- Greedy, Array, Implementation
- Solved
- No attempts yet
Problem
cards are laid out in a row from left to right. The card at position from the left has the integer . ()
You may remove some of the cards. The order of the cards that remain is kept.
For example, let and . If you remove the second and fifth cards, the remaining numbers from left to right are . So the number on the third remaining card from the left is .
After removing some cards, a remaining card is called a fixed card if the number on the -th remaining card from the left is exactly . If every remaining card is a fixed card, the arrangement is in the fixed state.
For example, let and . If you remove the first, fifth, and eighth cards, the remaining numbers are in order. Every remaining card is a fixed card, so the arrangement is in the fixed state.
If and , the only way to reach the fixed state is to remove every card so that no cards remain.
Removing all cards always results in the fixed state.
Write a program that computes the minimum number of cards that must be removed to reach the fixed state.
Input
The first line contains an integer .
The second line contains integers in order.
Output
Print the answer on the first line.
Constraints
- For every (), .