ZOJ 3778:Talented Chef(贪心?思维)
Talented Chef
Time Limit: 2 Seconds Memory Limit: 65536 KB
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 Aii steps. The steps of a dish should be finished sequentially. In each minute of the cooking, Coach Gao can choose at most MMM 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
题意
有n个菜,做每个菜需要ai步,每次最多可以做m个菜,求做完这n个菜最少需要多少时间
Solve
先提供一个超时的思路:将n个菜按照ai降序排序,排序后每次取出前m个菜,然后让这m个菜一直减到取出的最小的aii小于未取出的n−m个菜的最大的ai,然后将减少后的不等于0的ai放入序列中,重复上述操作,直到n个数全部为0。
时间复杂度大概为O(n×m)
不超时的算法:
将所有的ai加起来,sum=∑ai,然后sum除以m向上取整。
但是只有这样是不正确的:如果出现了⌈sum/m⌉<max(ai)的情况,那么所需的时间一定不会小于ai中的最大值,所以我们需要比较⌈sum/m⌉和max(ai)的值
证明:点这里吧,不太会证明。程序是照着超时的思路对拍出来的
Code
超时代码在最后
AC代码
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=(1<<30);
const ll INF=(1LL*1<<60);
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out1.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin>>t;
while(t--)
{
int n,m;
ll x;
cin>>n>>m;
ll sum=0;
ll maxx=0;
for(int i=0;i<n;i++)
{
cin>>x;
maxx=max(maxx,x);
sum+=x;
}
ll _=(sum - 1)/(1LL*m)+1;
cout<<max(maxx,_)<<endl;
}
return 0;
}
超时代码
/*************************************************************************
> Author: WZY
> School: HPU
> Created Time: 2019-04-20 09:07:07
************************************************************************/
#include<bits/stdc++.h>
using namespace std;
const int maxn = 4e4 + 10;
int a[maxn];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out2.txt", "w", stdout);
#endif
int t;
int x;
scanf("%d",&t);
while (t --)
{
int ans=0;
int n,m;
priority_queue<int,vector<int>,less<int> > que;
scanf("%d %d",&n,&m);
for (int i = 0;i < n;i ++)
{
scanf("%d",&x);
que.push(x);
}
while(!que.empty())
{
int cnt=0;
int minn;
for(int i=0;i<m;i++)
{
minn=que.top();
que.pop();
a[cnt++]=minn;
if(que.empty())
break;
}
int maxx;
if(que.empty())
maxx=0;
else
maxx=que.top();
int __=max(1,minn-maxx);
ans+=max(1,minn-maxx);
for(int i=0;i<cnt;i++)
{
a[i]=max(a[i]-__,0);
if(a[i])
que.push(a[i]);
}
}
printf("%d\n",ans);
}
return 0;
}
ZOJ 3778:Talented Chef(贪心?思维)的更多相关文章
- zoj 3778 Talented Chef(思维题)
题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...
- ZOJ 3778 Talented Chef(找规律,模拟计算,11届ACM省赛,简单)
题目链接 2014年浙江省赛C题,当时觉得难,现在想想这题真水.. 找规律: 若 最大的那个步骤数*m-总和>=0,那么答案就是 最大的那个步骤数 . 否则 就要另加上不够的数量,具体看代 ...
- ZOJ 3778 Talented Chef
题目链接 题意 : 这个人需要做n道菜,每道菜Ai步,他可以同时做M道不同的菜的其中一步,问你最少需要多少时间能做完所有的菜. 思路 : 这个题比赛的时候禁锢思路了,根本没想出来,就是当M > ...
- ZOJ 3778 Talented Chef 模拟 [ 祝愿明天省赛一帆风顺, ZJSU_Bloom WILL WIN : )
这题的意思是给你 n 道菜,第 i 道菜需要 Ai 步才能完成 每次你能对 m 道菜分别完成一步,请问最少需要几次? 这题暴力写肯定是不行的,去年省赛的时候就是没写出来这题,今天再把思路理一理吧. 首 ...
- ZOJ 3778 Talented Chief
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3778 题目 某人做菜很厉害,一分钟能同时完成最多m个菜的一道工序,输入菜的 ...
- Talented Chef ZOJ - 3778
As we all know, Coach Gao is a talented chef, because he is able to cook M dishes in the same time. ...
- Talented Chef(简单题,被我想的太复杂了,用复杂的方法当然会超时咯,由此可见,并非所有题都是想的越多越好)
Description As we all know, Coach Gao is a talented chef, because he is able to cook M dishes in the ...
- zoj3778 Talented Chef
As we all know, Coach Gao is a talented chef, because he is able to cook M dishes in the same time. ...
- Mike and distribution CodeForces - 798D (贪心+思维)
题目链接 TAG: 这是我近期做过最棒的一道贪心思维题,不容易想到,想到就出乎意料. 题意:给定两个含有N个正整数的数组a和b,让你输出一个数字k ,要求k不大于n/2+1,并且输出k个整数,范围为1 ...
随机推荐
- java Random()用法
1.random.nextInt() random.nextIn()的作用是随机生成一个int类型,因为int 的取值范围是 -2147483648--2147483647 ,所以生成的数也是处于这个 ...
- A Child's History of England.25
It was a September morning, and the sun was rising, when the King was awakened from slumber by the s ...
- day31 协程
day31 协程 一.死锁与递归锁 所谓死锁:是指两个或者两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产 ...
- Spark基础:(五)Spark编程进阶
共享变量 (1)累加器:是用来对信息进行聚合的,同时也是Spark中提供的一种分布式的变量机制,其原理类似于mapreduce,即分布式的改变,然后聚合这些改变.累加器的一个常见用途是在调试时对作业执 ...
- 利用python代码获取文件特定的内容,并保存为文档
说明:有段时间需要读取上百个文件的单点能(sp),就写了下面的代码(计算化学狗努力转行中^-^) import os.path import re # 1 遍历指定目录,显示目录下的所有文件名 def ...
- 打破砂锅问到底!HTTP和HTTPS详解
HTTP 引自维基百科HTTP:超文本传输协议(英文:HyperText Transfer Protocol,缩写:HTTP)是一种用于分布式.协作式和超媒体信息系统的应用层协议.HTTP是万维网的数 ...
- 节省内存的循环banner(一)
循环banner是指scrollview首尾相连,循环播放的效果,使用非常广泛.例如淘宝的广告栏等. 如果是简单的做法可以把所有要显示的图片全部放进一个数组里,创建相同个数的图片视图来显示图片.这样的 ...
- i++ 和 ++i 探究原理
先看一个例子: package com.test; public class AutoIncrement { public static void main(String[] args) { int ...
- 基于注解的方式搭建mybatis开发框架
1.创建工程 <groupId>com.hope</groupId> <artifactId>day01_eesy_01mybatis</artifa ...
- 软件开发生命周期(SDLC)
一.简介 软件开发生命周期又叫做 SDLC(Software Development Life Cycle),它是集合了计划.开发.测试和部署过程的集合.如下图所示 : 二.五个阶段 1.分析阶段: ...