码迷,mamicode.com
首页 > 其他好文 > 详细

hihocoder-Weekly224-Split Array

时间:2018-10-16 02:02:09      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:输入   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

标签:输入   min   cpp   you   integer   case   line   0ms   eve   

原文地址:https://www.cnblogs.com/zhang-yd/p/9795073.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!