#P10922. Happybob's Numbers (UBC001B)

Happybob's Numbers (UBC001B)

Problem Description

Happybob has nn numbers on the ground. The ii-th number is denoted as aia_i. Happybob is studying how to delete all of these numbers. Before he starts all operations, he has one chance to rearrange these numbers in any order. Then he will delete numbers in the following way:

  • Happybob has a deletion index hh (initially h=1h=1). He will set a new variable HH with value aha_h, and then for every positive integer ii satisfying hi<kh\le i<k, he performs aiai+1a_i\gets a_{i+1} (here kk is the current number of remaining numbers on the ground), and deletes the number aka_k. After that, he sets hh to HH.

  • If after any operation, hh is strictly greater than the current number of remaining numbers on the ground, then he will not be able to delete any more numbers.

Of course, with this deletion method, it may be impossible to delete all numbers. So he now asks you: what is the maximum number of numbers he can delete?

Input Format

The first line contains a positive integer tt, the number of test cases.

For each test case:

The first line contains a positive integer nn, the size of aa.

The second line contains nn positive integers, representing the elements in aa.

Output Format

Output tt lines. Each line contains a positive integer, the answer for the corresponding test case.

2
3
1 2 3
4
114 514 1919 810
3
1

Hint

Sample Explanation

For the first test case, happybob can sort the array aa as [2,3,1][2, 3, 1]. The deletion process is as follows:

Operation hh (after the operation) Numbers on the ground (after the operation)
Initial 11 [2,3,1][2, 3, 1]
11 22 [3,1][3, 1]
22 11 [3][3]
33 [][]

There are no numbers left on the ground, meaning a total of 33 numbers were deleted.

For the second test case, it can be proven that no matter how you rearrange aa, you can only delete one number.

Constraints

This problem has multiple test cases.

For 100%100\% of the testdata, it is guaranteed that 1t,n,n5×1051 \le t,n,\sum n\le 5 \times 10^5, 1ai1091\le a_i\le 10^9. Here n\sum n is the sum of nn over all test cases.

Translated by ChatGPT 5