#K52482. Sorting Cow Heights
Sorting Cow Heights
Sorting Cow Heights
You are given a list of n cows with their respective heights. In one operation, you can move a single cow from its current position to the beginning of the list. Your task is to compute the minimum number of such operations required to arrange the cows in non-decreasing order of their heights.
A useful observation is that if you identify the longest non-decreasing subsequence (LNDS) of cows, then those cows do not need to be moved. Thus, the answer can be derived by subtracting the length of the LNDS from the total number of cows:
\(\text{operations} = n - |\text{LNDS}|\)
Design an efficient algorithm that reads the input, computes the answer, and prints it.
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains a single integer
n
representing the number of cows. - The second line contains
n
space-separated integers representing the heights of the cows.
outputFormat
Output a single integer to standard output (stdout) representing the minimum number of operations required to sort the list of cow heights in non-decreasing order.
## sample5
4 3 2 6 5
3