POJ - 3104

Drying

Time limit : 2000 msMemory 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 ≤ 10^9). The third line contains k (1 ≤ k ≤ 10^9).

Output

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

Examples

sample input #1
3
2 3 9
5

sample input #2
3
2 3 6
5

sample output #1
3

sample output #2
2


题目大意:
Jane有一台烘干机,她想利用最短的时间把所有的湿衣服都烘干。烘干机上一次只能放一件衣服,放置的时间都是整数。衣服不在烘干机上每单位时间减少1点水分,在烘干机上的每单位时间减少k点水分。
解题思路:
1.假设一个mid时间,去测试mid时间内能否烘干所有衣服。用二分搜索的方法逐步找到mid的最小值。
2.对于一个给定的mid时间,可以将所有的衣服分为2类:
      ai<=mid 说明这件衣服不用占用烘干机,可以自然风干。
      ai>mid,那么想在mid时间内全部烘干就需要占用烘干机。假设占用烘干机xi时间,因为最后能烘干的话,就有 ai <= k xi+(mid - xi)成立,也就是xi >= (ai - mid) / (k-1)。
3.将所有的xi向上取整累加,如果超过mid,说明不可行;如果不超过mid则表示时间够用。(向上取整可以采用xi = (ai-mid + k-2)/(k-1)得到)
注意:当K=1的时候,需要单独处理,当然这也就相当于没有烘干机,所有衣服都是出于自然风干状态。


源代码

#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<sstream>
using namespace std;

int n, k,num;
int a[100000];

bool test(int mid) {
    int time = 0;
    for (int i = 0; i < n; i++) {
        if (a[i] > mid) {
            time = time + (a[i] - mid + k -2 ) / (k - 1) ;
        }
    }
    return time > mid;
}
int main() {
    int i;
    while (~scanf("%d", &n)) {
        int l=0, h=0, m;
        for (i = 0; i < n; i++) {
            scanf("%d", &a[i]);
            if (h < a[i])
                h = a[i];
        }
        scanf("%d", &k);
        if (k == 1)
            printf("%d\n", h);
        else {
            while (h >= l) {
                m = (h + l) >> 1;
                if (test(m))
                    l = m + 1;
                else
                    h = m - 1;
            }
            printf("%d\n", m);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值