#K89157. Minimum Soldiers to Move

    ID: 37468 Type: Default 1000ms 256MiB

Minimum Soldiers to Move

Minimum Soldiers to Move

You are given several test cases. For each test case, there are n soldiers standing in a line with certain heights. Your task is to determine the minimum number of soldiers to move so that the remaining sequence (the soldiers that are not moved) is in non-decreasing order in terms of their heights.

This is equivalent to finding the length of the longest non-decreasing subsequence (LNDS) in each test case and then calculating the difference between the total number of soldiers n and the LNDS. The problem may involve cases with equal heights, strictly decreasing or increasing sequences, and mixed orders.

Input/Output: The input is taken from stdin and the output is printed to stdout.

inputFormat

The input begins with an integer T representing the number of test cases. Each test case consists of the following lines:

  • The first line contains an integer n representing the number of soldiers.
  • The next line contains n space-separated integers, each representing the height of a soldier.

For example:

3
5
5 3 4 2 1
4
1 2 3 4
6
4 3 2 3 4 1

outputFormat

For each test case, output a single integer on a new line representing the minimum number of soldiers that must be moved to achieve a non-decreasing sequence of heights.

For example, the output for the sample input above would be:

3
0
3
## sample
3
5
5 3 4 2 1
4
1 2 3 4
6
4 3 2 3 4 1
3

0 3

</p>