FZU 2219 StarCraft (贪心 优先队列)

StarCraft

Time Limit: 1000 mSec    Memory Limit : 32768 KB

Problem Description

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.

Source

第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2219

题目大意:开始有m个员工,一共要建n个建筑,每个建筑需要ti时间,一个员工建一个建筑,建完就死掉,对于某个员工可以将他变成蛋,在k个单位时间后会变成两个员工,求建完n个建筑的最少时间

题目分析:设一变二的操作为分裂,易知建完n个建筑需要n-m次分裂,因为分裂和建筑是并行进行的,故让一次分裂后的其中一个去建房子,另一个如果还需要分裂则拿它去分裂,易得贪心策略:尽可能拿现有的去建耗费时间长的建筑,因为分裂也是需要时间的。如样例1,先用一个单位时间分裂,在1时刻拿一个去建5(建完到6时刻)与此同时另一个继续分裂,到时刻2又有2个,现在还剩2个建筑要建,那他们去建即可,建完分别为时刻3和时刻5,故总时长为6,本题需要根据结果来合并,根据以上的分裂过程可以得到合并的策略,先取两个最小的,将次小的加k,去掉最小的,合并n-m次,最后的最大值即为答案,显然用优先队列来维护最适合不过。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

struct cmp {
	bool operator() (int a, int b) {
		return a > b;
	}
};

priority_queue <int, vector<int>, cmp> q;

int main() {
	int T;
	scanf("%d", &T);
	while(T --) {
		int n, m, k, t;
		scanf("%d %d %d", &n, &m, &k);
		for(int i = 0; i < n; i++) {
			scanf("%d", &t);
			q.push(t);
		}
		int num = n - m;
		while(num > 0) {
			q.pop();
			q.push(q.top() + k);
			q.pop();
			num --;
		}
		while(q.size() != 1) {
			q.pop();
		}
		printf("%d\n", q.top());
		while(!q.empty()) {
			q.pop();
		}
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值