HihoCoder 1448 Split Array

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 long

using 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 ;
}

原文地址:https://www.cnblogs.com/Cantredo/p/9863679.html

时间: 2024-11-02 09:48:30

HihoCoder 1448 Split Array的相关文章

leetcode 659. Split Array into Consecutive Subsequences

You are given an integer array sorted in ascending order (may contain duplicates), you need to split them into several subsequences, where each subsequences consist of at least 3 consecutive integers. Return whether you can make such a split. Example

410. Split Array Largest Sum

Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays. Note: Given m satisfies the following cons

Leetcode: Split Array Largest Sum

Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays. Note: Given m satisfies the following cons

[LeetCode] Split Array Largest Sum 分割数组的最大值

Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays. Note:Given m satisfies the following const

[LeetCode] Split Array into Consecutive Subsequences 将数组分割成连续子序列

You are given an integer array sorted in ascending order (may contain duplicates), you need to split them into several subsequences, where each subsequences consist of at least 3 consecutive integers. Return whether you can make such a split. Example

410. Split Array Largest Sum 把数组划分为m组,怎样使最大和最小

[抄题]: Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays. Note:If n is the length of array, as

[LeetCode] 659. Split Array into Consecutive Subsequences 将数组分割成连续子序列

You are given an integer array sorted in ascending order (may contain duplicates), you need to split them into several subsequences, where each subsequences consist of at least 3 consecutive integers. Return whether you can make such a split. Example

[LeetCode] 805. Split Array With Same Average 用相同均值拆分数组

In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and

动态规划——Split Array Largest Sum

题意大概就是,给定一个包含非负整数的序列nums以及一个整数m,要求把序列nums分成m份,并且要让这m个子序列各自的和的最大值最小(minimize the largest sum among these m subarrays). Note:If n is the length of array, assume the following constraints are satisfied: 1 ≤ n ≤ 10001 ≤ m ≤ min(50, n) Examples: Input:num