(二分法)【POJ-3104】Drying

来源:互联网 发布:日语网络 编辑:程序博客网 时间:2024/05/22 04:39
Drying
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 18301 Accepted: 4615

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

Source

Northeastern Europe 2005, Northern Subregion


二分法,晾干中值mid时间,多出来的就是需要烘干的,把使用烘干的次数加起来,小于晾的时间就往左靠,减小中值加快干衣效率。


#pragma comment(linker,"/STACK:1024000000,1024000000")#include<iostream>#include<cstdio>#include<algorithm>#include<stack>#include<vector>#include<map>#include<queue>#include<deque>#include<set>#include<cmath>#include<cstring>#include<string>#define INF 0cxf3f3f3f#define PI acos(-1.0)#define LL long longusing namespace std;inline LL read();const int MAXN = 1e5+10;LL a[MAXN];int main(void){    LL n,mid,sum,maxv,k,ans,l,r;    while(~scanf("%lld",&n))    {        maxv = ans = 0;        for(int i = 1; i <= n; ++i)        {            a[i] = read();            if(a[i] > maxv)                maxv = a[i];        }        k = read();        if(!(1^k))            ans = maxv;        else        {            l = 1, r = maxv;            while(l <= r)            {                mid = (l+r)/2, sum = 0;                for(int i = 1; i <= n; ++i)                    if(a[i] > mid)                        sum += ceil((a[i]-mid)*1.0/(k-1));                if(sum <= mid)                {                    ans = mid;                    r = mid-1;                }                else                    l = mid+1;            }        }        printf("%lld\n",ans);    }}inline LL read(){    int x=0,f=1;char ch=getchar();    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}