Fishing Master 贪心+思维

2019CCPC秦皇岛站网络赛-1008

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?
Input
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

题意:有n条鱼需要抓,每抓一条鱼需要K的时间,抓鱼的时间不能做其他事。只有一口锅,且一次只能煮一条鱼,每条鱼需要煮ti的时间。抓到鱼后可以把鱼放到锅里了,或者等要等锅里的鱼煮好再放新的鱼,或者把鱼放下,继续去捕鱼。

思路:开始的第一次捕鱼时间k是必须用的,一次煮鱼的时间可能可以捕多条鱼,即(ti/k)条。如果只利用煮鱼时间(假设每条鱼煮完了,自动从锅里出来,假设不让锅闲着)抓到的鱼,大于剩下需要补的鱼,一共需要花费的时间就是k+ ∑ i = 1 n t i \sum_{i=1}^nti i=1nti。但可能捕到的鱼不够剩下的鱼,多了m条时间就是k+ ∑ i = 1 n t i \sum_{i=1}^nti i=1nti+mk。可以利用煮鱼剩余的时间(ti%k或 ti(ti 代码先计算抓鱼的时间,用优先队列保存剩余煮鱼的时间。通过剩余煮鱼前(n-cnt)大的剩余时间ts把鱼抓完,用了k时间,ts包括在内不必等了,就不需要加了(锅浪费k-ts时间)。最后不需要捕鱼了,把剩余煮鱼时间加上即可。

	#include
    #include
    #include
    using namespace std;
    typedef long long ll;
    const int maxn=1e5+10;
    int main(){
     
    	int t;
    	scanf("%d",&t);
    	while(t--){
     
    		ll n,k;
    		ll a;
    		scanf("%lld%lld",&n,&k);
    		priority_queue<int> q;
    		ll time=k; //总时间 
    		int cnt=0;
    		for(int i=1;i<=n;i++){
     
    			scanf("%lld",&a);
    			time+=a/k*k;
    			cnt+=a/k;
    			q.push(a%k);
    		}
    		while(cnt<q.size()-1){
       //q.size() k时间后 一共需要抓的鱼   //cnt k后利用煮鱼时间且不让超过每次煮鱼时间能够抓的鱼 
    			time+=k;		//利用煮鱼时间不足捕鱼的时间再浪费一点时间  花了k时间 
    			q.pop();	//在剩余煮鱼时间最多这样做 
    		}
    		while(q.size()){
     
    			time+=q.top(); //没有鱼需要抓了 等着煮的时间 
    			q.pop();
    		}
    		cout<<time<<endl;
    	}
    }

同学的代码

#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
ll tp[100005];
ll yu[100005];
void init()
{
     
	memset(tp,0,sizeof(tp));
	memset(yu,0,sizeof(yu));
}
bool cmp(int a,int b)
{
     
	return a > b;
}
int main()
{
     
	int t;
	scanf("%d",&t);
	//cin >> t;
	while(t--)
	{
     
		init();
		ll n,k;
		scanf("%lld%lld",&n,&k);
		//cin >> n >> k;
		ll  ans=0;
		ll num=0;
		for(int i=0;i<n;i++)
		{
     
			//cin >> tp[i];
			scanf("%d",&tp[i]);
			ans+=tp[i];
			yu[i]=tp[i]%k;
			num+=tp[i]/k;
		}
		ans += k;
		num++;
		if(num>=n)
		{
     
			//cout << ans << endl;
			printf("%lld\n",ans);
		}
		else
		{
     
			sort(yu,yu+n,cmp);
			for(int i=0;i<n-num;i++)
			{
     
				ans+=k-yu[i];
			}
			//cout << ans << endl;
			printf("%lld\n",ans);
		}
	}	
}

你可能感兴趣的:(例题)