hihocoder-Weekly224-Split Array

來源:互聯網
上載者:User

標籤:輸入   min   cpp   you   integer   case   line   0ms   eve   

hihocoder-Weekly224-Split Array

 

題目1 : 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

 

 

題解:

  很簡單的題目

  因為該數組是已經排好序的,而且其範圍是1-10^6 , 可以用一個數組記錄每一個數的cnt,然後順序便利一遍,就可以判斷。

  時間複雜度: O(10^6)  

 

 

#include <cstdio>  #include <cstring> #include <cstdlib> const int MAXN = 100000 + 10; #define max(a, b) (a)>(b)?(a):(b) #define min(a, b) (a)>(b)?(b):(a) int n, k, num[MAXN]; int main(){     int test_case;    scanf("%d", &test_case);      int x, min_x, max_x, tmp;       bool ans;     while(test_case--)    {    memset(num, 0, sizeof(num));     scanf("%d %d", &n, &k);      max_x = 0; min_x = MAXN;     for(int i=0; i<n; ++i)    {    scanf("%d", &x);     num[x]++;      max_x = max(max_x, x);      min_x = min(min_x, x);     }     if(n%k != 0){    printf("NO\n");    continue;     }      ans = true;     for(int i=min_x; i<=max_x; ++i)    {    if(num[i] > 0)    {    tmp = num[i];     for(int j=0; j<k; ++j)    {    if(i+j > max_x){    ans = false;     break;     }    num[i+j] -= tmp;     }    }else if(num[i] < 0)    {    ans = false;     break;     }    }    if(ans){    printf("YES\n");    }else{    printf("NO\n");    }    } } 

  

 

hihocoder-Weekly224-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.