#K43447. Contiguous Sorted Subarrays Division
Contiguous Sorted Subarrays Division
Contiguous Sorted Subarrays Division
You are given an array of n distinct integers and an integer k. Your task is to determine if the array can be partitioned into exactly k non-empty contiguous subarrays such that when the array is sorted, it forms at most k segments. More formally, let \(S = [s_1, s_2, \ldots, s_n]\) be the array sorted in ascending order. Define the original index of \(s_i\) as \(p_i\). We form a new segment whenever \(p_{i+1} \neq p_i + 1\) for \(1 \le i < n\). If the total number of segments \(\le k\), then output POSSIBLE
; otherwise, output IMPOSSIBLE
.
Note: When k is 1, the entire array must already be sorted in ascending order to be considered POSSIBLE
.
inputFormat
The input is given from standard input and is formatted as follows:
T n1 k1 a1,1 a1,2 ... a1,n ... nT kT aT,1 aT,2 ... aT,n
Where:
- T is the number of test cases.
- For each test case, the first line contains two integers n and k.
- The second line contains n distinct integers representing the array.
outputFormat
For each test case, print a single line containing either POSSIBLE
or IMPOSSIBLE
based on whether the array can be partitioned into at most k segments as described.
3
5 3
1 5 3 4 2
4 2
2 4 1 3
6 1
10 20 30 40 50 60
IMPOSSIBLE
IMPOSSIBLE
POSSIBLE
</p>