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 <= N, M <= 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
思路:这个贪心算是比较经典了?(逃... 假如,每次都刚好能减少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;
}

本文介绍了一道关于算法的问题——天才厨师。问题描述了一个厨师如何高效地同时烹饪多道菜肴,以达到最短的烹饪时间。该问题通过贪心算法进行解决,并提供了完整的代码实现。
1383

被折叠的 条评论
为什么被折叠?



