【FZU - 2219】StarCraft(贪心+优先队列)

本文探讨了一道关于《星际争霸2》中优化建筑建造时间的问题。初始有M个工人及N个建筑任务,每个工人完成任务后消失,可通过特殊能力在K时间后将一名工人转化为两名。文章提供了一个解决方案,通过贪心算法确定完成所有建筑任务所需的最短时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ZB loves playing StarCraft and he likes Zerg most!

One day, when ZB was playing SC2, he came up with an idea:

He wants to change the queen's ability, the queen's new ability is to choose a worker at any time, and turn it into an egg, after K units of time, two workers will born from that egg. The ability is not consumed, which means you can use it any time without cooling down.

Now ZB wants to build N buildings, he has M workers initially, the i-th building costs t[i] units of time, and a worker will die after he builds a building. Now ZB wants to know the minimum time to build all N buildings.

Input

The first line contains an integer T, meaning the number of the cases. 1 <= T <= 50.

For each test case, the first line consists of three integers N, M and K. (1 <= N, M <= 100000, 1 <= K <= 100000).

The second line contains N integers t[1] ... t[N](1 <= t[i] <= 100000).

Output

For each test case, output the answer of the question.

Sample Input

2
3 1 1
1 3 5
5 2 2
1 1 1 1 10

Sample Output

6
10

Hint

For the first example, turn the first worker into an egg at time 0, at time 1 there’s two worker. And use one of them to build the third building, turn the other one into an egg, at time 2, you have 2 workers and a worker building the third building. Use two workers build the first and the second building, they are built at time 3, 5, 6 respectively.

题意:

开始有N个工程,M个工人。一个工人干完一项工程就会死亡,你可以将一个人放在蛋壳中,然后经过k时间就会分裂出两个人。问完成工程的最短时间。

思路:

题目中的意思好像不能一次放多个人去分裂。有M个人,N个工程,则我们还需要N-M个工程。这道题目,最先想法,是让人先去干耗时长的工程,然后留一个人去分裂。分裂后得到的人,去干剩下的耗时尽量短的工程。

过程:分裂的n-m次,每次都选择花费时间最小的两个建筑,然后将次小值+K再放回总体中,n-m次分裂后,选择最大的值。

参考博客

这道题目,是一道贪心,也可以算是一道哈夫曼树的思想的应用。 

自己这道题wa了几次,是因为判断的时候只是写了while(kk),但是遇到kk<0的情况就跳不出循环了。

这里需要注意,因为经常简写习惯了。这样写需要考虑kk是不是会小于0.

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
using namespace std;
priority_queue<int,vector<int>,greater<int> > q;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		while(!q.empty()) q.pop();
		int n,m,k;
		scanf("%d%d%d",&n,&m,&k);
		for(int i=0;i<n;i++)
		{
			int a;
			scanf("%d",&a);
			q.push(a);
		}
		int kk=n-m;
		while(kk>0)//一开始工人数可能很多,会变成负数,出不来,报re注意这一点
		{
			q.pop();
			int t2=q.top();
			q.push(t2+k);
			q.pop();
			kk--;
		}
		while(q.size()>1)
		{
			q.pop();
		}
		printf("%d\n",q.top());
	}
	return 0;
} 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值