FZOJ Problem 2219 StarCraft

                                                                                                                                     FZOJ Problem 2219 StarCraft                                                                                                            

Accept: 226    Submit: 591
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. 

题意:刀锋女王有了一个新技能,即可以使一只工蜂重新进入孵化期,卵经过k的时间会孵化出两只工蜂,现在有N个建筑要建造,每个建筑都有相应的建造时间,且每建造一个建筑需要牺牲工蜂,问最少需要多少时间完成建造。

思路:哈夫曼树+优先队列,建造时间越长的建筑需要尽量早点建造,在工蜂数量不足以同时建造所有建筑的时候,尽量先建造时间长的建筑,而建造时间短的建筑可以先进性孵化,用孵化后的工蜂来建造,这样贪心,将所有建筑的建造时间压入最小堆,每次选出建造时间最短的建筑出队列,对两者的建造时间进行一次合并,合并之后的时间=两者建造时间的最大值+一次孵化的时间k,再次将这个时间压入队列,直至队列中元素数量不大于工蜂数量为止,此时选出队列中最大的时间即可。

AC代码:

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<queue>
#include<set>
#include<vector>
#include<cmath>
#include<cstring>
#include<functional>
using namespace std;
const int N_MAX = 100000 + 5;

int N, M,K;//N个建筑,M个农民,孵卵时间K
priority_queue<int,vector<int>,greater<int> >que;
int main() {
    int T;
    cin >> T;
    while (T--) {
        
        scanf("%d%d%d",&N,&M,&K);
        for (int i = 0; i < N;i++) {
            int t;
            scanf("%d",&t);
            que.push(t);
        }
        while (N>M) {//建筑多于农民时,每次合并建造时间最短的建筑
            que.pop();
            int t = que.top();
            que.pop();
            que.push(t + K);
            N--;
        }
        while (que.size()>1) {
            que.pop();
        }
        cout << que.top() << endl;
        que.pop();
     }
    return 0;
}

 

posted on 2017-06-09 17:02  ZefengYao  阅读(275)  评论(0编辑  收藏  举报

导航