Fishing Master(贪心)

Problem Description

Heard that eom is a fishing MASTER, you want to acknowledge him as your mentor. As everybody knows, if you want to be a MASTER’s apprentice, you should pass the trial. So when you find fishing MASTER eom, the trial is as follow:

There are n fish in the pool. For the i - th fish, it takes at least ti minutes to stew(overcook is acceptable). To simplify this problem, the time spent catching a fish is k minutes. You can catch fish one at a time and because there is only one pot, only one fish can be stewed in the pot at a time. While you are catching a fish, you can not put a raw fish you have caught into the pot, that means if you begin to catch a fish, you can’t stop until after k minutes; when you are not catching fish, you can take a cooked fish (stewed for no less than ti) out of the pot or put a raw fish into the pot, these two operations take no time. Note that if the fish stewed in the pot is not stewed for enough time, you cannot take it out, but you can go to catch another fish or just wait for a while doing nothing until it is sufficiently stewed.

Now eom wants you to catch and stew all the fish as soon as possible (you definitely know that a fish can be eaten only after sufficiently stewed), so that he can have a satisfying meal. If you can complete that in the shortest possible time, eom will accept you as his apprentice and say “I am done! I am full!”. If you can’t, eom will not accept you and say “You are done! You are fool!”.

So what’s the shortest time to pass the trial if you arrange the time optimally?

t
The first line of input consists of a single integer T(1≤T≤20), denoting the number of test cases.

For each test case, the first line contains two integers n(1≤n≤105),k(1≤k≤109), denoting the number of fish in the pool and the time needed to catch a fish.

the second line contains n integers, t1,t2,…,tn(1≤ti≤109) ,denoting the least time needed to cook the i - th fish.

Output
For each test case, print a single integer in one line, denoting the shortest time to pass the trial.

Sample Input
2
3 5
5 5 8
2 4
3 3

Sample Output
23
11

题意:
求钓鱼+炖鱼的时间最少,要求钓鱼的时候不能停止,可以炖鱼的时候去钓鱼。

题解:
我们分析下那些时间不能节约,钓第一条鱼+所有炖鱼时间不能节约,可以节约的是钓鱼的时间(除了第一条)。
我们计算下在炖鱼的时候可以完全钓多少鱼(num【i】/m累加和为p),
如果p>=n-1 说明我们在炖鱼的时候把鱼全钓了。
如果p<n-1 说明除了p条鱼在炖鱼的时候钓了, 还需要钓n-p-1条鱼,在钓这些鱼中,我们钓一条鱼,其中在钓鱼的过程中炖的鱼会熟,鱼熟了后到钓完鱼的时候这时锅里没有鱼,这段时间我们避免不了,但是我们要求最小时间,可以把段时间尽可能的小,把所有鱼的炖鱼时间%m放入优先队列,循环n-p-1次总和每次加上m-q.top()。m-最大的时间使浪费的时间最小。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue> 
typedef long long ll;
using namespace std;
const int mmax=1e5+10;
ll  num[mmax];
priority_queue<ll> q;
int  main()
{
	ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--)
	{
		while(!q.empty())
			q.pop();
		ll n,m;
		cin>>n>>m;
		ll sum=m,ans=1,ans0=0,p=0; 
		for(int i=1;i<=n;i++)
		{
			cin>>num[i];
			sum+=num[i];
			q.push(num[i]%m);
			p+=num[i]/m;	
		} 
		if(p<n-1)
		{
			for(int i=1;i<=n-p-1;i++)
			{
				sum+=m-q.top();
				q.pop();
			}
		}
		cout<<sum<<endl; 
	}
	return  0;
} 

发布了88 篇原创文章 · 获赞 30 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43667611/article/details/100145253
今日推荐