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

举报
小哈里 发表于 2022/05/11 00:13:05 2022/05/11
【摘要】 problem B. Moamen and k-subarrays time limit per test2 seconds memory limit per test256 megabytes inp...

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;
}



  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

文章来源: gwj1314.blog.csdn.net,作者:小哈里,版权归原作者所有,如需转载,请联系作者。

原文链接:gwj1314.blog.csdn.net/article/details/119581465

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。