Zoj 3781(构造)

Zoj 3781(构造)

Zoj 3781

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

将所有物品的重量看做一个整体,每次肯定是要平均地去掉m个,那么答案就是sum/m向上取整,同时还有一种特例,假如存在一个很大的数,那么答案肯定要覆盖整个很大的数才行,我们需要两者取其大

#include <iostream>
#include <cstdio>
using namespace std;
int main(){
     int T;
     scanf("%d",&T);
     while(T--){
        int n,m;
        scanf("%d%d",&n,&m);
        int sum=0;
        int x,mx=0;
        for(int i=0;i<n;i++){
            scanf("%d",&x);
            mx=max(mx,x);
            sum+=x;
        }
        m=min(n,m);
        int ans;
        if(sum%m==0) ans=sum/m;
        else ans=sum/m+1;
        printf("%d
",max(ans,mx));
     }
     return 0;
}

原文地址:https://www.cnblogs.com/zsyacm666666/p/6805733.html