poj3104 Drying

                                                                                                                                                                    Drying
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14519   Accepted: 3728

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


题意:Jane希望你计算出将所有的衣服都烘干的最短时间,每件衣服一开始都有ai的水分,自然状态下每件衣服在单位时间内都会减少一份水,并且jane有烘干机,烘干机每次只能烘一件衣服,使用机器烘衣服一个单位时间内可以让衣服减少k份水,现在需要让所有衣服的水分含量都降低至0,
至少需要多少时间。
思路:二分搜索,判断条件C:在d单位时间内能否将所有衣服烘干,true代表可以,false代表无法做到
条件如何去判断:整体的考虑,若d单位时间内所有衣服全部自然晾晒,则d单位时间内都会减少d份水,减去d份水后,再继续考虑还存在水分的衣服,这些衣服必须在d次机器烘干中全部失水,否则d单位时间无法做到使所有衣服失水,条件判断将为false,否则true
特别注意:k可以取1,故除以(k-1)会出现RE,此时需要单独考虑
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<numeric>
#include<cstring>
using namespace std;
typedef long long LL;
const int N_MAX = 100000 +4;
int  N;
LL a[N_MAX],k,aa[N_MAX];//k代表用吹风机后衣服每次失水量
bool C(LL d) {//判断用d天能否将所有衣服弄干
    memcpy(aa,a,sizeof(a));
    for (int i = 0; i < N; i++) {
        aa[i] -= d;
        if (aa[i] < 0)aa[i] = 0;
    }
    if (k == 1) {////!!!!!!!!!!!!!!!!!!!!k=1会RE
        LL M = *max_element(aa,aa+N);
        if (M == 0)return true;
        else return false;
    }
    LL num = 0;
    for (int i = 0; i < N;i++) {
        if (aa[i] > 0) { 
            if (aa[i] % (k - 1))
                num += (aa[i] / (k - 1) + 1);
            else
                num += aa[i] / (k - 1);
        }
        if (num > d)return false;
    }
    return true;
}

int main() {
        scanf("%d", &N);
        for (int i = 0; i < N; i++) {
            scanf("%lld", &a[i]);
        }
        scanf("%lld",&k);
        LL lb = *min_element(a,a+N)/k, ub = *max_element(a,a+N);
        while (ub - lb > 1) {
            LL mid = (lb + ub) / 2;
            if (C(mid))ub = mid;//说明mid天可以全干,也许还可以用更少的天数
            else lb = mid;
        }
        printf("%lld
", ub);
    
    return 0;
}
原文地址:https://www.cnblogs.com/ZefengYao/p/6373877.html