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 ...
随机推荐
- 工作学习1-tcp自连接
运维同事反馈服务起不来.下面为了方便,写了一个demo来展示. https://gitee.com/northeast_coder/code/tree/master/case/case1_tcp_se ...
- 日常Java 2021/9/27
题目: 在某个比赛中,有6个评委为参赛的选手打分,分数为1-100的随机整数.选手的最后得分为:除去最高分和最低分后的4个评委分值的平均值(不考虑小数部分). package m; import ja ...
- 深入理解mysql锁与事务隔离级别
一.锁 1.锁的定义 锁即是一种用来协调多线程或进程并发使用同一共享资源的机制 2.锁的分类 从性能上分类:乐观锁和悲观锁 从数据库操作类型上分类:读锁和写锁 从操作粒度上分类:表锁和行锁 2 ...
- Java【常用的日期操作】
目录 1.设置时间 2.获取年月日时分秒 3.通过运算获取时间 4.和Date类转换 5.格式化时间 6.新功能LocalDate:当前日期格式化 7.示例 java.util.Calendar 类是 ...
- VIM多标签页
:tabnew 增加一个标签 :tabc 关闭当前的tab :tabo 关闭所有其他的tab :tabp 或gT 前一个 :tabn 或gt 后一个 :tabs 显示 ...
- linux shell中的条件判断语句
http://bbs.chinaunix.net/thread-396805-1-1.html shell 判断语句 流程控制 "if" 表达式 如果条件为真则执行then后面的部 ...
- c学习 - 算法
简介: 一个程序包括两方面内容:数据结构.算法 数据结构:对数据的描述,包括数据的类型和数据的组织形式 算法:对操作的描述,即操作步骤 (程序=算法+数据结构) 算法是灵魂,数据结构是加工对象,语言是 ...
- Java Maven项目搭建
创建空项目 New Project --> Empty Project --> ... 配置JDK Project Settings --> Project 选择JDK Module ...
- MySQL(2):数据管理
一. 外键概念: 如果公共关键字在一个关系中是主关键字,那么这个公共关键字被称为另一个关系的外键.由此可见,外键表示了两个关系之间的相关联系.以另一个关系的外键作主关键字的表被称为主表,具有此外键的表 ...
- Ribbon详解
转自Ribbon详解 简介 Spring Cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具,它基于Netflix Ribbon实现.通过Spring Cloud的封装,可以让 ...