zoukankan      html  css  js  c++  java
  • Talented Chef(简单题,被我想的太复杂了,用复杂的方法当然会超时咯,由此可见,并非所有题都是想的越多越好)

    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

    2
    3 2
    2 2 2
    10 6
    1 2 3 4 5 6 7 8 9 10
    

    Sample Output

    3
    10

    source


    这道题比赛的时时候总是超时,因为每做一次菜我都把剩余的菜的步数排序,让他从步数最多的开始做,
    这样总共可能做40000道菜,即使我用的快排,复杂度也是特别高的

    第一行输入测试案例数t
    第二行输入厨师一共要做的菜数,和同一分钟能同时做的菜数
    求厨师做完这些菜所花的最短时间

    我的超时代码如下
    #include <stdio.h>
    #include <algorithm>
    #include <string.h>
    using namespace std;
    bool cmp(int a, int b)
    {
        return a > b;
    }
    int main()
    {
        int t, d[40010];
        scanf("%d",&t);
        while(t--)
        {
            int n, m, cnt = 0;
            scanf("%d%d",&n, &m);
            memset(d, 0, sizeof(d));
            for(int i = 0; i < n; i++)
                scanf("%d", &d[i]);
            sort(d, d + n, cmp);
            while(d[0] != 0)
            {
                for(int i = 0; d[i] != 0 && i < m; i++)
                {
                    d[i]--;
                }
                sort(d, d+n, cmp);
                cnt++;
            }
            printf("%d
    ", cnt);
        }
    }

    上边的代码中sort函数要用到的比较函数我总是写错,记住是 return a > b;

    后来发现不用这么麻烦,反正我的思想也是一刻都不让厨师闲着(每过一分钟都排序也是这个原因),能完全发挥本领就让他完全发挥本领,所以把 (所有菜的步骤数之和) / (厨师每分钟可以做的菜数) + (所有菜的步骤数之和) % (厨师每分钟最多做的菜数)    与   步骤数最多的菜的步骤数进行比较,较大的即为结果

    这样简单多了,自然不会超时,原是我想多了啊,哈哈

    #include <stdio.h>
    int main()
    {
        int t;
        scanf("%d", &t);
        while(t--)
        {
            int n, m, sum = 0, maxd = 0, a;
            scanf("%d%d", &n, &m);
            for(int i = 0; i < n; i++)
            {
                scanf("%d", &a);
                if(a > maxd)
                    maxd = a;
                sum += a;
            }
            a = sum / m;
            if(sum % m)
                a++;
            if(a > maxd)
                printf("%d
    ", a);
            else
                printf("%d
    ", maxd);
        }
        return 0;
    }
  • 相关阅读:
    easyui学习笔记3—在展开行内的增删改操作
    easyui学习笔记2—在行内进行表格的增删改操作
    4星|《贫穷的本质》:小额贷款对穷人帮助有限,助推政策也许是更好的
    科技类好书12本
    咨询公司等专业服务公司的权力结构:3星|《哈佛商业评论》第4期
    投资、投机、经济周期相关20本书,其中好书8本半
    3星|《进化:中国互联网生存法则》:点评十年来互联网公开大事
    黑洞有毛 or 黑洞无毛:4星|《环球科学》2019年03月号
    3星|《给产品经理讲技术》:APP开发技术介绍,没有技术背景的话恐怕只能看懂书中的比喻和结论
    2星|《为什么我们总是在逃避》:尝试用精神分析理论解释常见负面情绪
  • 原文地址:https://www.cnblogs.com/rain-1/p/4762217.html
Copyright © 2011-2022 走看看