Talented Chef(简单题,被我想的太复杂了,用复杂的方法当然会超时咯,由此可见,并非所有题都是想的越多越好)
2015-08-27 06:46
218 查看
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 <= 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
Sample Output
上边的代码中sort函数要用到的比较函数我总是写错,记住是 return a > b;
后来发现不用这么麻烦,反正我的思想也是一刻都不让厨师闲着(每过一分钟都排序也是这个原因),能完全发挥本领就让他完全发挥本领,所以把 (所有菜的步骤数之和) / (厨师每分钟可以做的菜数) + (所有菜的步骤数之和) % (厨师每分钟最多做的菜数) 与 步骤数最多的菜的步骤数进行比较,较大的即为结果
这样简单多了,自然不会超时,原是我想多了啊,哈哈
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 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\n", 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\n", a); else printf("%d\n", maxd); } return 0; }
相关文章推荐
- 极限运动
- PS学习笔记-----选区的作用即是:用来控制操作的范围
- [8月]总结
- 【LeetCode-面试算法经典-Java实现】【206-Reverse Linked List(反转一个单链表)】
- 【LeetCode-面试算法经典-Java实现】【205-Isomorphic Strings(同构字符串)】
- 【LeetCode-面试算法经典-Java实现】【204-Count Primes(统计质数)】
- php大力力 [024节]PHP中的字符串连接操作(2015-08-27)
- iOS开发——高级技术&签名机制
- iOS-提高iOS开发效率的方法和工具
- iOS开发——UI精选OC篇&UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍
- iOS开发——高级技术OC篇&运行时(Runtime)机制
- iOS开发——高级技术精选OC篇&Runtime之字典转模型实战
- iOS开发——高级技术&广告服务
- Spring MVC SimpleUrlHandlerMapping example
- iOS—Xcode 7真机测试
- [LeetCode#130]Surrounded Regions
- LeetCode Product of Array Except Self
- iOS完整学习路线图
- 开发中常见问题集锦
- Container With Most Water