#K93142. Distribute Books on Shelves
Distribute Books on Shelves
Distribute Books on Shelves
You are given t test cases. For each test case, you are provided with n books and k shelves. Each book is characterized by the number of pages it contains. Sophie wants to distribute the books on the shelves according to the following constraints:
- Each shelf must contain at least one book.
- The books on each shelf must be arranged in non-decreasing order of page numbers.
- The first k books (after sorting all books by page number) must be strictly increasing, i.e. for all \(0 \leq j < k-1\), the \(j\)th book must have fewer pages than the \((j+1)\)th book.
Formally, let \(a_1, a_2, \dots, a_n\) be the number of pages in each book. First, check if \(n < k\). If so, the answer is NO
because there are not enough books to place at least one on every shelf. Otherwise, sort the sequence and verify that \(a_j < a_{j+1}\) holds for all \(j\) from \(1\) to \(k-1\) (using 1-indexing). If the condition is satisfied, output YES
; otherwise, output NO
.
For each test case, output the corresponding result on a new line.
inputFormat
The first line of input contains the integer t, the number of test cases. Each test case is described as follows:
- A line containing two integers n and k, where n is the number of books and k is the number of shelves.
- A line containing n integers, representing the number of pages in each book.
outputFormat
For each test case, print a single line containing either YES
if it is possible to distribute the books according to the constraints, or NO
otherwise.
4
5 3
5 2 9 3 4
6 2
10 20 15 5 25 30
4 4
1 4 3 2
3 5
1 2 3
YES
YES
YES
NO
</p>