標籤:貪心 contains turn output break seve ret ref std
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。代碼如下(感覺還是寫得複雜了...而且基本思路也還沒有...還得繼續努力啊... (:зゝ∠) ):
#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