CF2176A.Operations with Inversions

传统题 时间 2000 ms 内存 256 MiB 3 尝试 1 已通过 1 标签

Operations with Inversions

Given an array a1,a2,,ana_1, a_2, \ldots, a_n. In one operation, you can choose a pair of indices i,ji, j such that 1i<jn1 \le i \lt j \le n, ai>aja_i \gt a_j, and remove the element at index jj from the array. After that, the size of the array will decrease by 11, and the relative order of the elements will not change.

Determine the maximum number of operations that can be performed on the array if they are applied optimally.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t501 \le t \le 50). The description of the test cases follows.

The first line of each test case contains an integer nn (1n1001 \le n \le 100) — the size of the initial array.

The second line of each test case contains nn natural numbers a1,a2,,ana_1, a_2, \ldots, a_n (1ain1 \le a_i \le n).

Output

For each test case, output the maximum number of operations that you can perform on the given array.

Note

In the first example, we can first choose the pair i=2i = 2, j=3j = 3 with values a2=2a_2 = 2, a3=1a_3 = 1 and remove a3a_3. The resulting array will be a=[3,2]a = [3, 2]. After that, we can remove the second element by choosing the pair i=1i = 1, j=2j = 2. The total number of operations is 22.

In the second, third, and fifth examples, no operations can be performed since there are no suitable pairs.

In the fourth example, we can remove the second and fifth elements. It can be shown that this is the optimal answer and there is no solution that removes more elements.

Samples

5
3
3 2 1
3
1 2 3
3
3 3 3
5
3 1 4 5 2
1
1
2
0
0
2
0

在线编程 IDE

建议全屏模式获得最佳体验