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.
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).
For each test case, output the least time (in minute) to finish all dishes.
2 3 2 2 2 2 10 6 1 2 3 4 5 6 7 8 9 10
310
就是一道想法题,一开始我想的就是求总和然后除以m就是它的最少的步数。
但是不是的,还要考虑到它烤煎饼的最大步数,其实就是那么多个饼中,烤最久的那个饼需要的步数。
虽然说要求的是最少的步数,但是必须要同时满足两个条件才可以。
所以最后求的就是两个当中的最大的那个。
#include<stdio.h> #include<string.h> int max(int a,int b){ return a>b?a:b; } int main(){ int T,i,j,n,m,sum,maxn,a; scanf("%d",&T); while(T--){ maxn=sum=0; scanf("%d%d",&n,&m); for(i=1;i<=n;i++){ scanf("%d",&a); sum+=a; //不停的求出较大的那个,然后把它存到maxn中去。 maxn=max(a,maxn); } int ans=0; //表示如果sum不能被m步整除,那么最后还要再加上一步。 if(sum%m) ans=sum/m+1; //如果可以整除,那么就直接sum除以m就好了; else if(sum%m==0) ans=sum/m; //返回两个中较大的那个; ans=ans>maxn?ans:maxn; printf("%d\n",ans); } }
原文地址:http://blog.csdn.net/acmer_hades/article/details/44132853