Div2 1557B 贪心思想、下标确定数组连续

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:

  1. Split the array into exactly k k k non-empty subarrays such that each element belongs to exactly one subarray.
  2. Reorder these subarrays arbitrary.
  3. 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 1t103) — 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 1kn105).

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 0ai109). 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 3105.

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:

  1. Split a a a into { [ 6 ] , [ 3 , 4 ] , [ 2 ] , [ 1 ] } \{ [6], [3, 4], [2], [1] \} {[6],[3,4],[2],[1]}.
  2. Reorder them: { [ 1 ] , [ 2 ] , [ 3 , 4 ] , [ 6 ] } \{ [1], [2], [3,4], [6] \} {[1],[2],[3,4],[6]}.
  3. 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[i1]+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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值