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 NNN dishes for the dinner. The iii-th dish contains AiA_iAi 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 TTT indicating the number of test cases. For each test case:
The first line contains two integers NNN and MMM (1&lt;=N,M&lt;=40000)(1 &lt;= N, M &lt;= 40000)(1<=N,M<=40000). The second line contains NNN integers AiA_iAi (1&lt;=Ai&lt;=40000)(1 &lt;= Ai &lt;= 40000)(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

题意

nnn个菜,做每个菜需要aia_iai步,每次最多可以做mmm个菜,求做完这nnn个菜最少需要多少时间

Solve

先提供一个超时的思路:将nnn个菜按照aia_iai降序排序,排序后每次取出前mmm个菜,然后让这mmm个菜一直减到取出的最小的aia_iai小于未取出的n−mn-mnm个菜的最大的aia_iai,然后将减少后的不等于000aia_iai放入序列中,重复上述操作,直到nnn个数全部为000
时间复杂度大概为O(n×m)O(n\times m)O(n×m)

不超时的算法
将所有的aia_iai加起来,sum=∑aisum=\sum a_isum=ai,然后sumsumsum除以mmm向上取整。
但是只有这样是不正确的:如果出现了⌈summ⌉&lt;max(ai)\lceil \dfrac {sum}{m}\rceil &lt;max(a_i)msum<max(ai)的情况,那么所需的时间一定不会小于aia_iai中的最大值,所以我们需要比较⌈summ⌉\lceil \dfrac {sum}{m}\rceilmsummax(ai)max(a_i)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;
}

转载于:https://www.cnblogs.com/Friends-A/p/11054955.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值