C

来源:互联网 发布:各个数据库的优缺点 编辑:程序博客网 时间:2024/05/20 22:01

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

23 22 2 210 61 2 3 4 5 6 7 8 9 10

Sample Output

310

题意:

每次在n个数字中最多可以取出m个数字,减去1,每次花费1个单位时间 .求最后都为0的最小时间。

思路:

一开始我们将问题复杂化了,想的是从小到大的顺序从某一个数到m个依次减一,但发现思路不对,其实 如果m>n,显然只要数字中最大的那个数字的次数。否则

 假设全部都能恰好满足全部m个数字, if(sum%m == 0) cur = sum / m。 如果不能,我就多一次  if( sum%m!=0 ) cur = sum / m +1;但是还是要和max比较。因为,这个数字至少是要>=max 的。取大的那一个即可。
细节:
别忘了不能被m整除时,需要加一!
代码:
#include <iostream>#include <cstdio>using namespace std;int main(){    int maxn=0,i,max,n,m,t;    int sum=0,a,s;    cin>>t;    while(t--)    {        sum=0;        maxn=0;        cin>>n>>m;        for(i=0;i<n;i++)        {            cin>>a;            if(maxn<a)            maxn=a;            sum+=a;        }        s=sum/m;        if(sum%m!=0)        {            s++;        }        max=maxn>s?maxn:s;        cout<<max<<endl;    }    return 0;}
心得:
有些看似很不正确的思路就是正确的。要大胆的尝试!!

0 0