CF #737(div2)B. Moamen and k-subarrays,贪心,下标

Posted 小哈里

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF #737(div2)B. Moamen and k-subarrays,贪心,下标相关的知识,希望对你有一定的参考价值。

problem

B. Moamen and k-subarrays
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Moamen has an array of 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 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 is a subarray of a sequence b if a can be obtained from 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 (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 and k (1≤k≤n≤105).

The second line contains n integers a1,a2,…,an (0≤|ai|≤109). It is guaranteed that all numbers are distinct.

It is guaranteed that the sum of n over all test cases does not exceed 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
inputCopy
3
5 4
6 3 4 2 1
4 2
1 -4 0 -2
5 1
1 2 3 4 5
outputCopy
Yes
No
Yes
Note
In the first test case, a=[6,3,4,2,1], and k=4, so we can do the operations as follows:

Split a into {[6],[3,4],[2],[1]}.
Reorder them: {[1],[2],[3,4],[6]}.
Merge them: [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 subarrays.

As an example, if we split it into {[1,−4],[0,−2]}, we can reorder them into {[1,−4],[0,−2]} or {[0,−2],[1,−4]}. However, after merging the subarrays, it is impossible to get a sorted array.

solution

题意:

  • 给出一个长为n的序列,将其分成k段连续的子数组,对这k段任意排序,求能否令原序列有序。

思路:

  • 考虑先记录原序列中每个值所在的位置,对原序列排序得到正确的序列,扫一遍正确的序列,对于当前值,如果它原先不在正确的位置上,那么去原序列找到他的位置,判断它后面的数有几个是可以连在当前的块上的,统计个数即可。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
int a[maxn], b[maxn];
int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int T;  cin>>T;
	while(T--){
		int n, k;  cin>>n>>k;
		map<int,int>ma;
		for(int i = 1; i <= n; i++)cin>>a[i], b[i]=a[i], ma[a[i]]=i;
		sort(b+1,b+n+1);
		int cnt = 0;
		for(int i = 1; i <= n; i++){
			cnt++;
			int j = ma[b[i]];
			for(; j+1 <= n; j++){
				if(b[i+1]==a[j+1])i++;
				else break;
			}
		}
		if(cnt<=k)cout<<"Yes\\n";
		else cout<<"No\\n";
	}
	return 0;
}


以上是关于CF #737(div2)B. Moamen and k-subarrays,贪心,下标的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #737 (Div. 2) 1557. C. Moamen and XOR(组合数,位)

CF #737(div2)A. Ezzat and Two Subsequences,贪心

cf1557 C. Moamen and XOR

CF #727(div2)B. Love Song,前缀和

CF #724(div2)B. Prinzessin der Verurteilung, BFS枚举

CF #738(div2)B. Mocha and Red and Blue(构造)