poj 3104 二分

来源:互联网 发布:淘宝首页怎么弄全屏 编辑:程序博客网 时间:2024/06/05 22:49

Drying
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 13601 Accepted: 3500

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 #132 3 95sample input #232 3 65

Sample Output

sample output #13sample output #22


题意:

有n件衣服都是湿的,分别需要a【i】时间去风干

现在可以用吹风机去吹,吹一分钟顶风干 k  分钟

使用吹风机的时候不计算风干的效果



题解:

所有衣服都干了最少要 1 分钟,最多需要max (a[ i ])分钟

然后在上面的这个范围枚举一个 中间值 mid  假设为最后风干的需要的时间

然后依据判断变换左右两边边界(left 和 right)的值

吹风机每一分钟顶 k -1 分钟风干时间

因此可以每时每刻都所有衣服进行风干,然后使用吹风机的时候每一分钟减少 k -1



对于样例

3

2  3  9

5

经过两分钟,衣服的湿度

0  1  7

此时用吹风机去吹需要3分钟

或者风干三分钟(为答案)

0  0  6

吹分机需要吹2分钟





#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define maxn 100100#define LL long longint n;LL a[maxn],k;bool deal(LL mid){    LL sum=0;    for(int i=0;i<n;i++){        if(a[i]>mid){            sum+=(a[i]-mid)/k;            if((a[i]-mid)%k)                sum++;            if(sum>mid)                return false;        }    }    return true;}int main(){    //freopen("in.txt","r",stdin);    while(scanf("%d",&n)!=EOF)    {        LL big=0,ans;        for(int i=0;i<n;i++){            scanf("%lld",&a[i]);            big=big>a[i]?big:a[i];        }        scanf("%lld",&k);        if(k==1){            printf("%lld\n",big);            continue;        }        LL left=1,right=big;        LL mid;        k--;        while(left<=right)        {            mid=(right-left)*0.5+left;            if(deal(mid))                right=mid-1,ans=mid;            else                left=mid+1;        }        printf("%lld\n",ans);    }    return 0;}


0 0
原创粉丝点击