1448: Split Array
Time Limit: 10000 ms
Single-point time limit: 1000 ms
Memory limit: 256 MB
Description
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 }.
Input
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)
Output
For each test case output yes or no in a separate line.
Sample Input
2
12 3
1 1 2 2 3 3 3 4 4 5 6
12 4
1 1 2 2 3 3 3 4 4 5 6
Sample output
Yes
No
Question
I have analyzed the question of "split array" in week 1 of hiho (new idea get !)
The question is to give an ordered array with a length of N and ask whether it can be divided into any subarrays with k elements (greater than zero.
We can solve this problem with greed. The specific idea is to find the minimum Minn in array a every time, starting with the minimum Minn, and find whether there are sub-arrays Minn, Minn + 1 in array, minn + 2, ·, Minn + k-1 and other elements. If one of the elements does not exist, no is output directly. If both exist, subtract these elements from array, repeat the above process to find the minimum value, starting with the minimum value .... when the elements are reduced to the end, that is, the elements in array a are reduced to zero. If no result is found, the output is yes. The Code is as follows (I think it is still complicated to write... and the basic idea is not... I still have to work hard...(: Too many rows)):
#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 longusing 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 ;}
Hihocoder 1448 split Array