ZOJ 3778 Talented Chef【贪心】

186 篇文章 0 订阅

Talented Chef

Time Limit: 2 Seconds       Memory Limit: 65536 KB

As we all know, Coach Gao is a talented chef, because he is able to cook M dishes in the same time. Tonight he is going to have a hearty dinner with his girlfriend at his home. Of course, Coach Gao is going to cook all dishes himself, in order to show off his genius cooking skill to his girlfriend.

To make full use of his genius in cooking, Coach Gao decides to prepare N dishes for the dinner. The i-th dish contains Ai steps. The steps of a dish should be finished sequentially. In each minute of the cooking, Coach Gao can choose at most M different dishes and finish one step for each dish chosen.

Coach Gao wants to know the least time he needs to prepare the dinner.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains two integers N and M (1 <= NM <= 40000). The second line contains N integers Ai (1 <= Ai <= 40000).

Output

For each test case, output the least time (in minute) to finish all dishes.

Sample Input
2
3 2
2 2 2
10 6
1 2 3 4 5 6 7 8 9 10
Sample Output
3
10

Author:  JIANG, Kai
Source:  The 11th Zhejiang Provincial Collegiate Programming Contest


题目大意:给你n道菜,每分钟可以一起搞定m道菜的一个步骤,和n道菜的每一道菜需要进行的步数,询问最小时间花费。


分析样例:对于样例1、

2 2 2

我们第一分钟解决1 2的一个步骤之后变成:
1 1 2

我们第二分钟解决1 3的一个步骤之后变成:
0 1 1

我们第三分钟解决 2 3的一个步骤之后变成:
0 0 0

所以是3.


分析:对于这个题,一开始的思路当然是贪心的将每一分钟的操作都不浪费的去做菜就能达到最贪心的点,也就是最小消耗、辣么这个时候的最小消耗就是:sum/m(如果是整除)sum/m+1(如果是不整除)。

但是如果有这样的一组数据:

3 2

1 1 10000

按照刚刚的公式明显不对,那是因为什么呢?因为10000太大,导致不能每一次都达到最贪心的点,也就是说,把1号和2号菜做好之后呢,专心做3号菜就行,然而3号菜不能拆分出来,所以这个时候的最小时间消耗是10000.因为在这10000时间里边,可以分配出操作来搞定1号和2号菜。

所以最终能够形成的思路就是:

如果sum/m(当然不整除的时候需要+1)得到的值小于最难做的那道菜的步数值,辣么输出那最难做的那道菜的值,否则输出sum/m(当然整除的时候要+1);

思路说了这么久,其实其中最关键的点就是在分析有没有把一道菜的值过分大,以至于我们在计算sum/m的时候,把这个菜当做很多菜来做的情况。


总结:如果有这样的情况:其他菜都做完了,只剩下这一个菜还需要很多步骤去做。辣么我们在sum/m的时候,把这个菜当做很多菜来求值,不合法,应该输出这道最难做的菜需要的步数。因为其实我们在做这个最难做的菜的时候 ,就能分配出操作把其他好做的菜都已经做完了。如果没有刚刚所述的情况,这种情况也就是在最后一次操作之前,都能做到不浪费任何操作(每一分钟都是操作m个菜),最后也能够一次对其他剩下的菜都进行操作完毕的情况。

AC代码;

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int main()
{
    int t;
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            int n,m;
            scanf("%d%d",&n,&m);
            int maxn=0;
            int sum=0;
            for(int i=0;i<n;i++)
            {
                int k;
                scanf("%d",&k);
                maxn=max(k,maxn);
                sum+=k;
            }
            int output;
            if(sum%m==0)output=sum/m;
            else output=sum/m+1;
            if(output<=maxn)
            {
                printf("%d\n",maxn);
            }
            else printf("%d\n",output);
        }
    }
}










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值