HihoCoder 1448 Split Array

1448 : Split Array

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

描述

You are given an sorted integer array A and an integer K. Can you split A into several sub-arrays that each sub-array has exactly K continuous increasing integers.

For example you can split {1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6} into {1, 2, 3}, {1, 2, 3}, {3, 4, 5}, {4, 5, 6}.

输入

The first line contains an integer T denoting the number of test cases. (1 <= T <= 5)

Each test case takes 2 lines. The first line contains an integer N denoting the size of array A and an integer K. (1 <= N <= 50000, 1 <= K <= N)

The second line contains N integers denoting array A. (1 <= Ai <= 100000)

输出

For each test case output YES or NO in a separate line.

样例输入

2
12 3
1 1 2 2 3 3 3 4 4 5 5 6
12 4
1 1 2 2 3 3 3 4 4 5 5 6

样例输出

YES
NO

题解

参考了hiho一下第224周《Split Array》题目分析(新思路get!)
题目大意是给一个长为 n 的有序数组,问是否能将其分成任意份(大于零..)含有 k 个元素的连续递增的子数组。
可以用贪心解决,具体思路就是每一次寻找 A 数组内的最小值 minn ,以最小值 minn 为起点,找 A 数组内是否有子数组 minn , minn + 1 , minn + 2 , ····, minn + k - 1 等元素,如果其中一个元素不存在,则直接输出 NO , 如果均存在,在 A 数组中减去这些元素,继续重复以上过程,寻找最小值,以最小值为起点….当元素减少到最后,即 A 数组元素减少到零时,若都没有出现不能找到的情况,则代表可以达到要求输出 YES。代码如下(感觉还是写得复杂了…而且基本思路也还没有…还得继续努力啊… (:зゝ∠) ):

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <utility>
#define ll long long
#define ull_ unsigned long long

using namespace std ;

int cnt[100005] ;

int main(){
int t ;
cin >> t ;
while ( t -- ){
memset(cnt , 0 , sizeof(cnt)) ;
int n , k ;
cin >> n >> k ;
for ( int i = 0 ; i < n ; i ++ ){
int x ;
cin >> x ;
cnt[x] ++ ;
}
bool check = true ;
int time = n ;
while ( time > 0 ){
int minn = 100005 ;
for ( int i = 0 ; i <= 100000 ; i ++ ){
if ( cnt[i] != 0 ){
minn = i ;
break ;
}
}
int num = 0 ;
for ( int i = 0 ; i < k ; i ++ ){
if ( cnt[minn + num] == 0 ){
check = false ;
break ;
}else{
cnt[minn + num] -- ;
}
num ++ ;
}
time -= k ;
}
if ( check ){
cout << "YES" << endl ;
}else{
cout << "NO" << endl ;
}
}
return 0 ;
}