fzu 2219 很好的二分题呀
 Problem 2219 StarCraft

Accept: 278    Submit: 798
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.


题意:  给你n个大楼所需要的建筑的时间,你的任务就是找出工人去盖楼,每个大楼一个工人盖就可以了,但是每个工人如果盖一个大楼的话,盖完他就会离开人世, 每个工人在某个时间可以不去盖楼,  而是花费 k  个单位的时间去  拉来另一个工人,你要求出 盖完这些大楼的最短时间.

思路:  比赛的时候并没有想到,虽说想到了二分,但是并没有 去深想。。  其实我们可以去二分时间然后得到每个任务最晚在哪天开始。 想到这里我们就可贪心的使得 每个任务 在最晚的那一天开始,,其他的天数  尽可能去拉来更多的工人。看是否可以在  mid 的天数内完成任务。。。  但是注意  每个工人  每k 天可以拉来一个工人  二分的天数可能很大,,所以我们应该尽可能的使他小  那么最少就应该用  n*k  天  , 但是还是特别大,, 所以我们以  k  个时间单位 为一个单位。

代码:  

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define N 200005

using namespace std;

typedef long long ll;

ll t[N];
ll time1[N];
ll n,m,k;


int jud(ll lim)
{
	memset(time1,0,sizeof(time1));
	for(int i=1;i<=n;i++)
	{
		if(lim-t[i]<0) return 0;
		ll in=min((ll)n,(lim-t[i])/k);
		time1[in]++;
	}
	
	ll res=m; 
	
	for(int i=0;i<=n;i++)  // 以k  为单位时间的   时间度量 
	{
		//printf("time *k : %d  res : %d\n",i,res);
		res-=time1[i]; 
		if(res<0) return 0;
		res*=2;
		if(res>n) return 1;
	}
	
	return 1;
}

int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		cin>>n>>m>>k;
		for(int i=1;i<=n;i++) scanf("%I64d",&t[i]);
		
		ll ans=0;
		ll l=0;  ll r=1e18+5;
		
		//jud(9);
		
		
		while(l<=r)
		{
			ll mid=(l+r)>>1;
			//printf("mid :  %lld \n",mid);
			if(jud(mid))
			{
				ans=mid;
				r=mid-1;
			}
			else l=mid+1;
		}
						
		cout<<ans<<endl;
	}
	
	return 0;
}

阅读更多
文章标签: 二分
个人分类: 二分
想对作者说点什么? 我来说一句

fzu 1698解题报告

2009年04月14日 968B 下载

没有更多推荐了,返回首页

不良信息举报

fzu 2219 很好的二分题呀

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭