B - Drying (POJ - 3104) 二分专题。。

It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She is not afraid of this boring process. Jane has decided to use a radiator to make drying faster. But the radiator is small, so it can hold only one thing at a time.

Jane wants to perform drying in the minimal possible time. She asked you to write a program that will calculate the minimal time for a given set of clothes.

There are n clothes Jane has just washed. Each of them took ai water during washing. Every minute the amount of water contained in each thing decreases by one (of course, only if the thing is not completely dry yet). When amount of water contained becomes zero the cloth becomes dry and is ready to be packed.

Every minute Jane can select one thing to dry on the radiator. The radiator is very hot, so the amount of water in this thing decreases by k this minute (but not less than zero — if the thing contains less than k water, the resulting amount of water will be zero).

The task is to minimize the total time of drying by means of using the radiator effectively. The drying process ends when all the clothes are dry.

Input

The first line contains a single integer n (1 ≤ n ≤ 100 000). The second line contains ai separated by spaces (1 ≤ ai ≤ 109). The third line contains k (1 ≤ k≤ 109).

Output

Output a single integer — the minimal possible number of minutes required to dry all clothes.

Sample Input

sample input #1
3
2 3 9
5

sample input #2
3
2 3 6
5

Sample Output

sample output #1
3

sample output #2
2

题意:有一些衣服,每件衣服有一定水量,有一个烘干机,每次可以烘一件衣服,每分钟可以烘掉k滴水。每件衣服没分钟可以自动蒸发掉一滴水,用烘干机烘衣服时不蒸发。问最少需要多少时间能烘干所有的衣服。

分析:

数据范围大,如果每分钟用暖气片烘含水最多的那件,果断TLE,要用二分。最大理论时间是水最多的那件自然风干的时间,以及最小理论时间1分钟。然后判断这个时间是否满足条件。设含水量大于 t 的第i件衣服所带的 水量 为Ai ,需要用 吹风机的 次数 为 X ,用于 风干 的时间为 Y,吹风机一分钟 能够 吹干的水量为 k,则 可得到 以下两式 :  X    +    Y    =    t    ; X    +    Y * k    >=   Ai  ;      由以上两式可得 : Y  >=  (Ai - t)/(k - 1)       算出所有含水量大于 t 的衣服 的 最小的 Y , 然后相加得到 sum ,再用 sum 与  t 比较 ,作为 二分中 上下界改变的条件 。

有个技巧就在于二分的时间在算的时候首先减去这个时间作为自然风干,然后用烘干机的话每分钟就只能掉k-1的水(因为烘干的时候是不会自然风干的,然后需要特殊处理k=1的情况)

#include <iostream>
#include<algorithm>
#include<cmath>
#include<math.h>
using namespace std;
long long a[10005];
long long n,k;
long long int judge(long long int mid)
{
	long long int sum = 0;
	for(int i=1;i<=n;i++)
	{
		if (a[i] > mid) //ceil函数求大于等于表达式的最小整数,因此若除出小数必进1
		sum += ceil((a[i] - mid)*1.0 / (k - 1));//计算要使用几分钟暖气片才能烘干
	}
    //返回使用暖气片的总时间
	return sum;
}
int main()
{
	int i;
	scanf("%lld", &n);
	for (i = 1; i <= n; i++)
	scanf("%lld", &a[i]);
	scanf("%lld", &k);
	sort(a + 1, a + n + 1);
	if (k == 1)printf("%d\n", a[n]);
    else 
    {
       long long int low = 1, high = a[n];//初始化 
	    while(low<=high)
   	{
		long long int mid = (low + high) / 2; //从统计函数得到使用暖气片的时间并同理论最短时间比较,给出新的mid值
        long long int time = judge(mid);
		if(time<=mid)	high = mid - 1;
		else if(time>mid)  low = mid + 1;//如果等于继续搜索未被改变的端点值 
	}
        printf("%lld\n",low);
   }
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_42079027/article/details/81264515