day6

贪心算法:求局部最优解

感觉就是多做题吧,上题!

Fishing Master

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.

ac代码
#include <bits/stdc++.h>
using namespace std;
int T,n,m;
int a[100010];
bool cmp(int a,int b){
	return a%m>b%m;
}
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		int num=1;
		scanf("%d%d",&n,&m);
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			num+=a[i]/m;
		}
		sort(a,a+n,cmp);
		long long time=m;
		if(num>=n) //鱼已经够了,不需要再捕捉鱼了
		{
			for(int i=0;i<n;i++) time+=a[i];
		}
		else    //鱼的数量还不够,还需要继续捕捉
		{
			for(int i=0;i<n;i++)
			{
				if(i<n-num) time+=(int)ceil((double)a[i]/(double)m)*m;
				else time+=a[i];
			}
		}
		cout<<time<<endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C:\Users\admin\Desktop\前端开发\Node.js\day6\code\api_server\node_modules\mysql\lib\protocol\Parser.js:437 throw err; // Rethrow non-MySQL errors ^ Error: secretOrPrivateKey must have a value at module.exports [as sign] (C:\Users\admin\Desktop\前端开发\Node.js\day6\code\api_server\node_modules\jsonwebtoken\sign.js:107:20) at Query.<anonymous> (C:\Users\admin\Desktop\前端开发\Node.js\day6\code\api_server\router_handler\2user.js:49:26) at Query.<anonymous> (C:\Users\admin\Desktop\前端开发\Node.js\day6\code\api_server\node_modules\mysql\lib\Connection.js:526:10) at Query._callback (C:\Users\admin\Desktop\前端开发\Node.js\day6\code\api_server\node_modules\mysql\lib\Connection.js:488:16) at Sequence.end (C:\Users\admin\Desktop\前端开发\Node.js\day6\code\api_server\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24) at Query._handleFinalResultPacket (C:\Users\admin\Desktop\前端开发\Node.js\day6\code\api_server\node_modules\mysql\lib\protocol\sequences\Query.js:149:8) at Query.EofPacket (C:\Users\admin\Desktop\前端开发\Node.js\day6\code\api_server\node_modules\mysql\lib\protocol\sequences\Query.js:133:8) at Protocol._parsePacket (C:\Users\admin\Desktop\前端开发\Node.js\day6\code\api_server\node_modules\mysql\lib\protocol\Protocol.js:291:23) at Parser._parsePacket (C:\Users\admin\Desktop\前端开发\Node.js\day6\code\api_server\node_modules\mysql\lib\protocol\Parser.js:433:10) at Parser.write (C:\Users\admin\Desktop\前端开发\Node.js\day6\code\api_server\node_modules\mysql\lib\protocol\Parser.js:43:10) Node.js v18.12.1
06-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值