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 = + ; 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 = ; 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()>) {
que.pop();
}
cout << que.top() << endl;
que.pop();
}
return ;
}

FZOJ Problem 2219 StarCraft的更多相关文章

  1. FZU 2219 StarCraft(星际争霸)

    Description 题目描述 ZB loves playing StarCraft and he likes Zerg most! One day, when ZB was playing SC2 ...

  2. FZOJ Problem 2150 Fire Game

                                                                                                        ...

  3. FZOJ Problem 2148 Moon Game

                                                                                                  Proble ...

  4. FZOJ Problem 2107 Hua Rong Dao

                                                                                                        ...

  5. FZOJ Problem 2110 Star

                                                                                                        ...

  6. FZOJ Problem 2103 Bin & Jing in wonderland

                                                                                                        ...

  7. FZU-2219 StarCraft(贪心)

     Problem 2219 StarCraft Accept: 294    Submit: 860Time Limit: 1000 mSec    Memory Limit : 32768 KB   ...

  8. 【BZOJ】【2219】数论之神

    中国剩余定理+原根+扩展欧几里得+BSGS 题解:http://blog.csdn.net/regina8023/article/details/44863519 新技能get√: LL Get_yu ...

  9. CSU 2018年12月月赛 G(2219): Coin

    Description 有这样一个众所周知的问题: 你面前有7个硬币,其中有一个劣质的(它比正常的硬币轻一点点),你有一个天平,问需要你需要使用天平多少次能保证找到那个劣质的硬币. 众所周知的算法是: ...

随机推荐

  1. Python——函数的高级应用

    一.函数赋值给变量 函数也是对象,也可以赋值给变量,当把函数赋值给变量后,可以通过变量调用函数. 例: def test(x,y): return x * y # 把函数赋值给变量 a = test ...

  2. LLDB详解

    LLDB的Xcode默认的调试器,它与LLVM编译器一起,带给我们更丰富的流程控制和数据检测的调试功能.平时用Xcode运行程序,实际走的都是LLDB.熟练使用LLDB,可以让你debug事半功倍 L ...

  3. 如何用纯 CSS 创作一个极品飞车 loader

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/MBbEMo 可交互视频 ...

  4. jQuery 淡入淡出有png图的时候 ie8下有黑色边框

    jQuery fadeTo 时ie8 png图片有黑色边框 往带有png图的样式里加 filter:progid:DXImageTransform.Microsoft.AlphaImageLoader ...

  5. Leetcode 81. 搜索旋转排序数组 II

    题目链接 https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/description/ 题目描述 假设按照升序排序的数 ...

  6. 基于百度OCR的图片文字识别

    先上图,有图有真相 首先在百度开通ORC服务,目前是免费的,普通识别每天50000次免费,非常棒! 百度文档:http://ai.baidu.com/docs#/OCR-API/top 下载百度SDK ...

  7. css 透明度使用

    设置元素整体透明度: div{ opacity: 0.5; } 设置背景色透明度 div{ background: rgba(0,0,0,0.5); }

  8. JAVA-基础(十) Swing

    在看到applet和Swing的时候,我想起了winform,以及java beans包中各种所谓的组件的时候,一切都那么似曾相识. Swing是AWT的扩展,它提供了更强大和更灵活的组件集合. 除了 ...

  9. Selenium WebDriver- 操作浏览器的cookie

    #encoding=utf-8 import unittest import time from selenium import webdriver from selenium.webdriver i ...

  10. TensorFlow L2正则化

    TensorFlow L2正则化 L2正则化在机器学习和深度学习非常常用,在TensorFlow中使用L2正则化非常方便,仅需将下面的运算结果加到损失函数后面即可 reg = tf.contrib.l ...