码迷,mamicode.com
首页 > 其他好文 > 详细

L-Talented Chef(数学题)

时间:2015-03-08 15:48:08      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:acm   模拟题   数学题   

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 <= 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

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);
	}
}


 

L-Talented Chef(数学题)

标签:acm   模拟题   数学题   

原文地址:http://blog.csdn.net/acmer_hades/article/details/44132853

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!