Description
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
source
这道题比赛的时时候总是超时,因为每做一次菜我都把剩余的菜的步数排序,让他从步数最多的开始做,
这样总共可能做40000道菜,即使我用的快排,复杂度也是特别高的
第一行输入测试案例数t
第二行输入厨师一共要做的菜数,和同一分钟能同时做的菜数
求厨师做完这些菜所花的最短时间
我的超时代码如下
#include <stdio.h> #include <algorithm> #include <string.h> using namespace std; bool cmp(int a, int b) { return a > b; } int main() { int t, d[40010]; scanf("%d",&t); while(t--) { int n, m, cnt = 0; scanf("%d%d",&n, &m); memset(d, 0, sizeof(d)); for(int i = 0; i < n; i++) scanf("%d", &d[i]); sort(d, d + n, cmp); while(d[0] != 0) { for(int i = 0; d[i] != 0 && i < m; i++) { d[i]--; } sort(d, d+n, cmp); cnt++; } printf("%d ", cnt); } }
上边的代码中sort函数要用到的比较函数我总是写错,记住是 return a > b;
后来发现不用这么麻烦,反正我的思想也是一刻都不让厨师闲着(每过一分钟都排序也是这个原因),能完全发挥本领就让他完全发挥本领,所以把 (所有菜的步骤数之和) / (厨师每分钟可以做的菜数) + (所有菜的步骤数之和) % (厨师每分钟最多做的菜数) 与 步骤数最多的菜的步骤数进行比较,较大的即为结果
这样简单多了,自然不会超时,原是我想多了啊,哈哈
#include <stdio.h> int main() { int t; scanf("%d", &t); while(t--) { int n, m, sum = 0, maxd = 0, a; scanf("%d%d", &n, &m); for(int i = 0; i < n; i++) { scanf("%d", &a); if(a > maxd) maxd = a; sum += a; } a = sum / m; if(sum % m) a++; if(a > maxd) printf("%d ", a); else printf("%d ", maxd); } return 0; }