ZOJ 3778 Talented Chef (贪心)

27 篇文章 1 订阅


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

题目大意:有一个人要做n道菜,给出每道菜所需步骤数。已知这个人每次同时能做m道菜的1步。问最少需要几次把这些菜做完。

思路:这个贪心算是比较经典了?(逃...    假如,每次都刚好能减少m次(每次都可以煮m个不同的菜), 最优解就是 sum/m,如果sum%m!=0就再+一次。这样贪心的意思就是菜比较多 也比较均匀。。如果有一种 1 1 1 1 100这种的呢,如果sum/m <= maxa[i], 说明每次不是m个不同的, 他煮到一半,就把某一个菜 “拆成”m个不同的菜了。。 因为最少最少 也要煮maxa[i]分钟。。如果sum/m <= maxa[i],说明菜种不够,那最多的那个肯定可以每分钟都有位置烧菜。。。 所以是maxa[i]...如果大于 maxa[i]说明他没有拆开。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 4e4+5;
typedef long long ll;
int sum[maxn], a[maxn], n, m;

int main(void)
{
    int t;
    cin >> t;
    while(t--)
    {
        cin >> n >> m;
        int tmax = 0, sum = 0;
        for(int i = 1; i <= n; i++)
            scanf("%d", &a[i]), tmax = max(tmax, a[i]), sum += a[i];
        int k = min(n, m);
        int tnum = (sum)/k;
        if(sum%k) tnum++;
        if(tmax > tnum) tnum += tmax-tnum;
        printf("%d\n", tnum);
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值