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?

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

Hint
Case 1: Catch the 3rd fish (5 mins), put the 3rd fish in, catch the 1st fish (5 mins), wait (3 mins),
take the 3rd fish out, put the 1st fish in, catch the 2nd fish(5 mins),
take the 1st fish out, put the 2nd fish in, wait (5 mins), take the 2nd fish out.
Case 2: Catch the 1st fish (4 mins), put the 1st fish in, catch the 2nd fish (4 mins),
take the 1st fish out, put the 2nd fish in, wait (3 mins), take the 2nd fish out.

题意:
有n条鱼等着你去钓,你可以自定义钓每条鱼的顺序,但是钓每条与都需要花费k分钟。钓到鱼之后你还需要把鱼煮熟,煮熟每条鱼花费的时间是ti 可是你只有一口锅,而且锅很小,每次只能装下一条鱼。你在钓鱼时是什么都不能做的,也即是说一旦开始钓鱼就必须在k分钟后才能停下来。当你停下来时如果锅里的鱼还没熟,你不能把鱼拿出来,你可以选择等一会,等到鱼熟了再把新的鱼放进去煮,也可以选择把生鱼存起来直接去钓下一条鱼。问你从开始钓鱼到把所有的鱼煮熟需要花费多少时间。

思路:首先计算煮鱼时间大于抓鱼时间时能抓多少条鱼,将零碎时间用优先队列存起来,如果煮鱼的时候不能把所有鱼抓上来就用零碎时间更大的去抓鱼,把所有鱼抓上来后,直接等就好了

#include
using namespace std;
typedef long long ll;
priority_queueq;
int main(){
	int t; scanf("%d",&t);
	while(t--){
		int n,k; scanf("%d%d",&n,&k);
		ll tim=k; //抓第一条鱼的时间 
		for(int i=1;i<=n;i++){
			int time; scanf("%d",&time);
			if(time>=k){ //煮鱼时间大于抓鱼时间,那么这个时间可以选择去抓鱼 
				tim+=time/k*k; //抓鱼时间 
				q.push(time%k); //零碎时间,用优先队列存起来 
			}
			else q.push(time%k); 
		} 
		int tmp=tim/k; //煮鱼的时间能抓多少条鱼
		while(tmp

你可能感兴趣的:(Fishing Master)