FZU 2219 StarCraft(优先队列贪心)
2016-11-01 22:29
330 查看
思路:手推一下样例大概猜到这样一个贪心,n个工作m个工人,那么需要n-m次分裂,每次分裂出来的一个人去做时间最长的工作,剩下的一个继续分裂,直到工人的数量够了,其实就是一个小根堆,每次取两个元素出来,将次小的元素+k再扔回去即可
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
priority_queue<int,vector<int>,greater<int> >q;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
int ans = 0;
for(int i = 1;i<=n;i++)
{
int x;
scanf("%d",&x);
q.push(x);
}
while(q.size()>m)
{
q.pop();
int tmp = q.top();
q.pop();
q.push(tmp+k);
}
while(q.size()!=1)
q.pop();
printf("%d\n",q.top());
while(!q.empty())
q.pop();
}
}
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
(1 <= t[i] <= 100000).
Output
For each test case, output the answer of the question.
Sample Input
Sample Output
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.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
priority_queue<int,vector<int>,greater<int> >q;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
int ans = 0;
for(int i = 1;i<=n;i++)
{
int x;
scanf("%d",&x);
q.push(x);
}
while(q.size()>m)
{
q.pop();
int tmp = q.top();
q.pop();
q.push(tmp+k);
}
while(q.size()!=1)
q.pop();
printf("%d\n",q.top());
while(!q.empty())
q.pop();
}
}
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
(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.
相关文章推荐
- FZU2219 StarCraft--贪心+优先队列
- FZU 2219 StarCraft (贪心 优先队列)
- FZU 2219 StarCraft(贪心+优先队列)
- FZU - 2219 StarCraft (哈夫曼树,贪心+优先队列)
- [BZOJ3523][[Poi2014]Bricks][贪心+优先队列]
- POJ 3190 Stall Reservations 【贪心 优先队列】
- FZU 2219【贪心】
- 优先队列:POJ No 3614 Sunscreen 贪心
- Poj 3614 Sunscreen【贪心+优先队列】
- 2431Expedition POJ- 优先队列 + 贪心
- 通过一道贪心学到了优先队列的用法
- 贪心+优先队列-51nod-1428 活动安排问题
- Codeforces 767E Change-free【贪心+优先队列】
- 【贪心+优先队列】POJ3190-Stall Reservations
- 51Nod - 1475 优先队列 + 贪心
- 【贪心+优先队列】51Nod 1053 最大M子段和 V2
- POJ2431 Expedition【贪心+优先队列】
- hdu 2850 Load Balancing (优先队列 + 贪心)
- Codeforces 913DToo Easy Problems (优先队列 & 贪心)
- 【CodeForces - 864】 A B C【模拟】 D【贪心+优先队列】