ZOJ 3778 Talented Chef(贪心))

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 Output3 10

分析:将结果设为ans,sum为ai的和 如果n<=m 那么ans为ai的最大值。
当n>m时:在取数的时候,始终从步骤最多的开始取。1.我们首先假设除了最后一次,每次取都能恰好取到m个,那么ans=sum/m, 如果最后一次取不到m即sum%m!=0时,ans++;
2.如果中间某一次就不能取到m个的话 那么剩下的次数就为ai的最大值(当前的),又因为我们取数的时候总是从最大的开始取,前面的每个过程它都有参与。故综合 ans=ai的最大值(一开始的)

 

最后我们可以得到 当可能性1成立的时候 ans>maxx 当可能性2成立的时候ans<maxx  所以我们始终取它们的最大值即可

代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    int t;
    ll n,m,ans,maxx,a,sum;
    std::ios::sync_with_stdio(false);
    cin>>t;
    while(t--)
    {
        sum=0;
        maxx=0;
        ans=0;
        cin>>n>>m;
        for(int i=0;i<n;i++)
        {
            cin>>a;
            maxx=max(maxx,a);
            sum+=a;
        }
        if(n<=m)
        {
            cout<<maxx<<endl;
            continue;
        }
        else
        {
           ans=sum/m;
           if(sum%m!=0)
           ans++;
           ans=max(maxx,ans);
           cout<<ans<<endl;
        }
    }
}

 

posted @ 2017-04-21 12:39  hinata_hajime  阅读(161)  评论(0编辑  收藏  举报