hihocoder-Weekly224-Split Array
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