B. Moamen and k-subarrays
Moamen has an array of n n n distinct integers. He wants to sort that array in non-decreasing order by doing the following operations in order exactly once:
- Split the array into exactly k k k non-empty subarrays such that each element belongs to exactly one subarray.
- Reorder these subarrays arbitrary.
- Merge the subarrays in their new order.
A sequence a a a is a subarray of a sequence b b b if a a a can be obtained from b b b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Can you tell Moamen if there is a way to sort the array in non-decreasing order using the operations written above?
Input
The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 3 1 \le t \le 10^3 1≤t≤103) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n n n and k k k ( 1 ≤ k ≤ n ≤ 1 0 5 1 \le k \le n \le 10^5 1≤k≤n≤105).
The second line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an ( 0 ≤ ∣ a i ∣ ≤ 1 0 9 0 \le |a_i| \le 10^9 0≤∣ai∣≤109). It is guaranteed that all numbers are distinct.
It is guaranteed that the sum of n n n over all test cases does not exceed 3 ⋅ 1 0 5 3\cdot10^5 3⋅105.
Output
For each test case, you should output a single string.
If Moamen can sort the array in non-decreasing order, output “YES” (without quotes). Otherwise, output “NO” (without quotes).
You can print each letter of “YES” and “NO” in any case (upper or lower).
Example
input
3
5 4
6 3 4 2 1
4 2
1 -4 0 -2
5 1
1 2 3 4 5
output
Yes
No
Yes
Note
In the first test case, a = [ 6 , 3 , 4 , 2 , 1 ] a = [6, 3, 4, 2, 1] a=[6,3,4,2,1], and k = 4 k = 4 k=4, so we can do the operations as follows:
- Split a a a into { [ 6 ] , [ 3 , 4 ] , [ 2 ] , [ 1 ] } \{ [6], [3, 4], [2], [1] \} {[6],[3,4],[2],[1]}.
- Reorder them: { [ 1 ] , [ 2 ] , [ 3 , 4 ] , [ 6 ] } \{ [1], [2], [3,4], [6] \} {[1],[2],[3,4],[6]}.
- Merge them: [ 1 , 2 , 3 , 4 , 6 ] [1, 2, 3, 4, 6] [1,2,3,4,6], so now the array is sorted.
In the second test case, there is no way to sort the array by splitting it into only 2 2 2 subarrays.
As an example, if we split it into { [ 1 , − 4 ] , [ 0 , − 2 ] } \{ [1, -4], [0, -2] \} {[1,−4],[0,−2]}, we can reorder them into { [ 1 , − 4 ] , [ 0 , − 2 ] } \{ [1, -4], [0, -2] \} {[1,−4],[0,−2]} or { [ 0 , − 2 ] , [ 1 , − 4 ] } \{ [0, -2], [1, -4] \} {[0,−2],[1,−4]}. However, after merging the subarrays, it is impossible to get a sorted array.
简述题意
题目给出长度为 n n n的数组,要求我们将其分割为 k k k份
最后判断是否可以将分割为K份的子数组排列从而保证数组有序
解题思路
初始想法
统计一共出现了多少次单调递增序列的情况,判断结果和K的关系
这一种思想是不对的,例如 6 3 7 6\ 3\ 7 6 3 7 按照我们的想法我们会将其分割为 [ 6 , [ 3 , 7 ] ] [6,[3,7]] [6,[3,7]],这样无论如何拼接结果都是不对的
经过5次的WA(考虑原值),我放弃了!!!
借鉴了一下别人的想法,我恍然大悟!
对于每一个值,我们可以通过下标的形式对每一个值进行标记
例如样例: 6 3 4 2 1 6\ 3\ 4\ 2\ 1 6 3 4 2 1经过标记后得到的新数组 5 3 4 2 1 5\ 3\ 4\ 2\ 1 5 3 4 2 1
接着我们只需要判断标记的值是否连续
如果出现了 a [ i ] = a [ i − 1 ] + 1 a[i] = a[i - 1] + 1 a[i]=a[i−1]+1说明他们是放在一个子数组中,否则的话划分为多个数组
最后比较划分的次数和K值的大小,来确认正确与否
C o d e Code Code
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
#define str string
#define all(x) begin(x),end(x)
struct Node{
int x,u;
};
inline void solve() {
int n,k,cnt = 0; cin >> n >> k;
vector<Node> a(n + 1);
for(int i = 1;i <= n;i++) {cin >> a[i].x; a[i].u = i;}
sort(begin(a) + 1,end(a),[&](Node b,Node c) {
return b.x < c.x;
});
for(int i = 2;i <= n;i++) {
if(a[i].u != a[i - 1].u + 1) cnt++;
}
if(cnt > k) cout << "No" << endl;
else cout << "Yes" << endl;
}
int main(){
// freopen("input.txt","r",stdin);
int t; cin >> t;
while(t--) solve();
// solve();
return 0;
}