#K67472. K-Reversal Sort
K-Reversal Sort
K-Reversal Sort
You are given an array A of n integers and allowed to perform exactly k operations. In each operation, you select a contiguous subarray of length m and reverse it. Your task is to determine whether it is possible to sort the entire array in non-decreasing order using exactly these k reversal operations.
The reversal operation is defined as follows: for a chosen index i (1 ≤ i ≤ n-m+1), reverse the segment A[i, i+m-1]. The array is considered sorted if and only if A[1] ≤ A[2] ≤ … ≤ A[n].
It can be mathematically shown that the array is sortable by exactly k operations if and only if $$k \times m \geq n.$$
inputFormat
The input consists of two lines:
- The first line contains three space-separated integers: n, k, and m — the size of the array, the number of reversal operations, and the length of the subarray to be reversed, respectively.
- The second line contains n space-separated integers representing the array A.
outputFormat
Output a single line containing either YES
if it is possible to sort the array using exactly k reversal operations, or NO
otherwise.
5 2 3
4 2 3 1 5
YES
</p>