现在的位置: 首页 > 综合 > 正文

POJ 3104 Drying (神题 啊 ~) — from lanshui_Yang

2018年02月21日 ⁄ 综合 ⁄ 共 2278字 ⁄ 字号 评论关闭

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 kthis 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

     题目大意不多说,主要说说 思想 。 刚开始做这道题时,就卡了好久,好不容易 想出办法 ,结果 提交 后 TLE ,哎,最后 还是 上网 搜了一下 大神们的 解题报告 ,才知道此题需用 二分 ,不得不感叹 大神们 的智慧啊 !! 以下说说本人的见解, 此题的思路是:二分 时间 ,假设 时间 为 t,那么含水量小于或等于 t 的衣服直接风干就行了,不需考虑,设含水量大于 t 的第i件衣服所带的 水量 为Ai ,需要用 吹风机的 次数 为 X ,用于 风干 的时间为 Y,吹风机一分钟 能够 吹干的水量为 k,则 可得到 以下两式 :                                                                                                    X    +    Y    =    t      ;                                                               X    +    Y * k    >=   Ai  ;                           由以上两式可得 : Y  >=  (Ai - t)/(k - 1)       算出所有含水量大于 t 的衣服 的 最小的 Y , 然后相加得到 sum ,再用 sum 与  t 比较 ,作为 二分中 上下界改变的条件 。具体用法的 详解 请看程序:             
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
long long a[100005];   // 注意 : 此题 需用 long long ,用 int 会 WA !!
int main()
{
    int n;
    scanf("%d",&n);
    int i;
    long long  max = -1;
    for(i=1; i<=n; i++)
    {
        scanf("%lld",&a[i]);
        if(max < a[i])           // 找出 含水量的最大值 
            max = a[i];
    }
    long long k ;
    scanf("%lld",&k);
    if (k == 1)       //k == 1 的情况 需要单独判断 !!!
    {
        printf("%lld\n",max);
    }
    else
    {
        long long left = 1,right = max,mid;        // 用含水量的最大值 作为 上边界
        while (right > left)
        {
            mid = (left + right)/2;
            int j ;
            long long sum =0;
            for(j=1; j<=n; j++)
            {
                if(a[j] > mid)
                {
                    long long tmp = ceil((a[j]-mid) * 1.0 / (k-1));  // ceil 是 向上取整 的函数 
                    sum += tmp;
                }
            }
            if(sum > mid)     // 此处 是 ">"  , 不要写成 ">=" !! 
            {
                left = mid + 1;
            }
            else
            {
                right =  mid; // WA 了无数次 原来 是这里出错了,可千万不能写成 right = mid - 1 !!!!
            }
        }
        mid = ( right + left )/2;
        printf("%lld\n",mid);
    }
    return 0;
}

抱歉!评论已关闭.