POJ3104 Drying (二分的力量2)

本文探讨了一种使用小型暖气加速衣物干燥的过程优化方法。通过算法计算,旨在利用有限的暖气资源,在最短时间内完成所有衣物的干燥。文中详细介绍了如何通过排序和二分查找等算法策略,有效地分配暖气使用时间,以达到干燥衣物的目的。
摘要由CSDN通过智能技术生成


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

3
2 3 9
5
3
2 3 6
5

Sample Output

3
2
题目大意:有许多衣服洗完需要晒干,每件衣服有一定的含水量,自然风干的话,每个单位时间含水量减一,使用吹风机的话,每个单位时间含水量减K,但一次只能对一件衣服使用吹风机,且使用吹风机的话就不再有自然风干的效果。问所有衣服都晒完的话,最短时间为多少?
一开始用的贪心模拟(每次吹含水量最多的那件),果断超时。后来看了网上大神的解答想到了二分,对时间进行二分,以a数组表示每件衣服的含水量,首先对a数组进行排序由于最长时间为max(a),最短时间为1,那我假设需要的时间为mid=(max+1)/2,那么对于含水量小于Mid的衣服就让他自然风干(不一定是最优解,但先假设是这样),然后对含水量大于mid的衣服挨个使用吹风机,直到每件衣服的含水量小于Mid为止,若这些时间加起来小于mid,那么说明在mid的时间内还可以用吹风机吹更多的衣服,所以答案在mid左边,反之在mid右边。
总之核心就是要找到能完全自然风干的衣服的最大时间和需要使用吹风机的衣服所要吹的时间的平衡点,此时需要的时间最少(这也符合贪心策略)。这样的思想可以解决很多最大值最小或者最小值最大的问题,因为此时单纯贪心已不再适用。
贪心代码(超时):(网上找的)
#include <iostream>
#include <cmath>
#include <algorithm>;
#include <cstdio>
using namespace std;

__int64 a[100003], k, n;

int dich( int l, int r )
{
    __int64 m = ( l + r ) / 2,  sum = 0;

    if( l>=r )  return m;

    for( int i=n-1; a[i]>=m; i-- )
        sum += (int)ceil( (a[i]-m)*1.0 / (k-1) );

    if( sum > m )
        return dich( m+1, r );
    else return dich( l, m );
}

int main()
{

    while( cin >> n ){
        for( int i=0; i<n; i++ )
            cin >> a[i];
        cin >> k;  sort( a, a+n );

        if( k==1 )  cout << a[n-1] << endl;
        else  cout << dich( 1, a[n-1] ) << endl;
    }
}
二分(AC)
#include <iostream>
#include <stdio.h>
#include  <algorithm>
#include <cmath>

using namespace std;

int main()
{
    long long flag,k,i,a[100004],left,right,mid,maxnum,n,t,sum=0;
    while(scanf("%lld",&n)!=EOF)
    {
    flag=0;
    sum=0;
    maxnum=-1;
    for (i=0;i<=n-1;i++)
    {
        scanf("%lld",&a[i]);
        if (a[i]>maxnum)
        {
            maxnum=a[i];
        }
    }
    scanf("%lld",&k);
    if (k==1)
    {
        printf("%lld\n",maxnum);
        flag=1;
    }
    sort(a,a+n);
    left=1;
    right=maxnum;
    while(right-left>0&&flag==0)
    {
        mid=(left+right)/2;
        sum=0;
        for (i=0;i<=n-1;i++)
        {
            if (a[i]>mid)
            {
                t=ceil((a[i]-mid)*1.0/(k-1));
                sum=sum+t;
            }
        }
        if(sum>mid)
        {
            left=mid+1;
        }
        else
        {
            right=mid;
        }
    }
    mid=(right+left )/2;
    if(flag==0)
    {
        printf("%lld\n",mid);
    }
    }
    return 0;
}

 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值