HihoCoder 1448 Split Array

來源:互聯網
上載者:User

標籤:貪心   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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.