#C5405. Array Modification to Non-decreasing Order
Array Modification to Non-decreasing Order
Array Modification to Non-decreasing Order
You are given an array of integers and an integer k. Your task is to determine whether the array can be modified into a non-decreasing (sorted in ascending order) array by changing at most k entries. When changing an entry, you can directly assign it the corresponding value from the sorted (non-decreasing) version of the array.
More formally, let the original array be \(A = [a_1, a_2, \dots, a_n]\) and let \(B\) be the sorted (non-decreasing) version of \(A\). Calculate the number of positions \(i\) where \(a_i \neq b_i\). If \(\text{number of differences} \leq k\), then it is possible to modify the array within the allowed number of changes; otherwise, it is not possible.
If it is possible to achieve the sorted order with at most k changes, output "YES" and print the modified (sorted) array. Otherwise, output "NO".
Note: The input and output must be handled via standard input (stdin) and standard output (stdout).
inputFormat
The first line contains an integer T, the number of test cases. Each test case consists of two lines:
- The first line contains two integers n and k, where n is the number of elements in the array, and k is the maximum number of allowed modifications.
- The second line contains n space-separated integers representing the array.
outputFormat
For each test case, output the result on separate lines:
- If it is not possible to modify the array within the allowed number of changes, print "NO".
- If it is possible, print "YES" on one line, and on the next line, print the modified (i.e., sorted) array with its elements separated by spaces.
2
5 1
5 3 4 8 2
4 0
1 2 3 4
NO
YES
1 2 3 4
</p>