ACM刷题之POJ————Drying

版权声明:本文为小时的枫原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaofeng187/article/details/79922939
Drying
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 20641   Accepted: 5202

Description

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


一个二分的题目,把时间二分,如果水分小于二分的时间,那么久不用考虑,如果大于,那么再去用吹风机吹。

假设X1是吹风耗费的时间, 那么 mid - X1 就是自然烘干的时间。

那么要烘干一件水分多的衣服要花费的时间就是  k * X1 + (mid - X1) >= a[i] 

其中a[i]就是水分多的衣服的水量。

移项后得出 X1 >= (a[i] - mid)/(k-1)

其中 k-1 不能为0,特判一下。

然后注意用ceil来向上取值。将X1 求和 与 mid值比较就能得出答案了。

注意X1的和可能爆int.


下面是ac代码

#include <stdio.h>  
#include <string.h>  
#include <math.h>  
#include <iostream>  
#include <algorithm>  
using namespace std;  
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
const double PI = acos(-1.0);
const int INF = 1e5+10;
const int N=2e5+7;

int a[N];
int n,k; 

bool check(int m) {
	__int64 sum = 0;
	for (int j = 0 ; j < n; j ++) {
		if (a[j] <= m) continue;
		
		sum += ceil((double)(a[j] - m) / (k - 1));
	}
	if (sum <= m) return 1;
	else return 0;
}

int main()
{
	//freopen("f:/input.txt", "r", stdin);
	while(scanf("%d", &n) != EOF) {
		int maxn = -INF;
		for (int i = 0; i < n; i ++){
			scanf("%d", &a[i]);
			if (a[i] > maxn) maxn = a[i];
		} 
		
		scanf("%d", &k);
		// 特判 
		if (k == 1) {
			
			printf("%d\n", maxn);
			continue;
		}
		int l = 0, r = maxn, mid;
		while (l < r) {
			mid = (l + r) >> 1;
			if (check(mid)) {
				r = mid;
			} else {
				l = mid + 1;
			}
		}
		
		printf("%d\n", l);
	}
}

猜你喜欢

转载自blog.csdn.net/xiaofeng187/article/details/79922939