POJ-3104 Drying

Drying
Time Limit: 2000MS Memory Limit: 65536K
   
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

Source

Northeastern Europe 2005, Northern Subregion
————————————————————词穷的分割线————————————————————
思路:学习二分逼近的时候,拿来练手的题目……当然我等弱菜只能先学习再内化了……思想类似于好斗的牛那题。但是check稍微难度加大了。首先考虑自然风干的问题。
自然风干和吹风机是同时进行的,因此 k-1 才是吹风机真正的效果。我们在二分答案的时候,答案为最小时间,然而吹风机每一分钟都在投入使用,因此答案其实就是吹风机的使用次数——假设我们看做每分钟使用一次(每分钟吹一件衣服,下一分钟就可能换一件)。check一下,枚举衣服。只有当水量大于吹风机使用次数(思考一下,每分钟都在使用吹风机的话,每件衣服自然风干的水量是多少)的时候,该衣服必须使用吹风机X次。X必须是剩余水量除以吹风机效果再向上取整。那么一旦该衣服需要使用的次数 > 当前二分的答案,check失败。
代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
int water[100005], cloth, k;
bool check(int m) {
    int cnt = 0; //计算需要使用吹风机的次数
    for(int i = 0; i < cloth; i++) {
        if(water[i] <= m)   continue;//自然风干
        else {
            cnt += (int)ceil((double)(water[i] - m) / (k-1));//该衣物使用吹风机次数
            if(cnt > m) return false;
        }
    }
    return true;
}
int main() {
    int maxi = 0;
    scanf("%d", &cloth);
    for(int i = 0; i < cloth; i++) {
        scanf("%d", water+i);
        if(water[i] > maxi) maxi = water[i];
    }
    scanf("%d", &k);
    if(k > 1) {
        int l = 1, r = maxi, mid;
        while(l <= r) {
            mid = (l+r) >> 1;
            if(check(mid))  r = mid - 1;
            else            l = mid + 1;
        }
        printf("%d\n", l);
    }
    else    printf("%d\n", maxi);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值