zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }
  • 相关阅读:
    232. Implement Queue using Stacks
    231. Power of Two
    n&(n-1)位运算的妙用
    230. Kth Smallest Element in a BST
    关于UNIX的exec函数
    Go语言的各种Print函数
    Go语言的接口interface、struct和组合、继承
    Go语言知识点笔记
    Ubuntu自定义终端窗口位置
    Python的类变量和成员变量、类静态方法和类成员方法
  • 原文地址:https://www.cnblogs.com/a249189046/p/6743019.html
Copyright © 2011-2022 走看看