poj 3104 dring 二分

Drying
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7684   Accepted: 1967

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

 

 

 1 /*

 2 

 3 题意:烤衣服,每分钟水量少k。自然干,每分钟少1.

 4            给10^5件衣服的水量。

 5            求最小的时间,全部的衣服都干了。

 6 不能理解成,烤衣服的时候,也自然干。k已经包括了。

 7 

 8 二分时间。对于时间mid判断能否实现。

 9 

10 

11 */

12 

13 

14 #include<iostream>

15 #include<stdio.h>

16 #include<cstring>

17 #include<cstdlib>

18 #include<algorithm>

19 using namespace std;

20 

21 typedef __int64 LL;

22 LL a[100005];

23 

24 bool fun(LL x,LL n,LL m)

25 {

26     LL i,num=0,cur;

27     if( m==1 )

28     {

29         for(num=-1,i=1;i<=n;i++)

30         if(a[i]>num) num=a[i];

31     }

32     else

33     {

34         for(i=1,num=0;i<=n;i++)

35         {

36             cur=a[i]-x;

37             if( cur<=0) continue;

38             if(cur%(m-1)==0)

39                 num=num+cur/(m-1);

40             else num=num+cur/(m-1)+1;

41         }

42     }

43     if( num<=x)return true;

44     else return  false;

45 }

46 int main()

47 {

48     LL n,i,l,r,mid,m;

49     bool cur;

50     while(scanf("%I64d",&n)>0)

51     {

52         for(i=1;i<=n;i++)

53             scanf("%I64d",&a[i]);

54         scanf("%I64d",&m);

55         for(i=1,l=1,r=0;i<=n;i++)

56         {

57             if(a[i]%m==0)

58             r=r+a[i]/m;

59             else r=r+a[i]/m+1;

60         }

61         while(l<r)

62         {

63             mid=(l+r)/2;

64             cur=fun(mid,n,m);

65             if( cur==true)

66                 r=mid;

67             else l=mid+1;

68         }

69         printf("%I64d\n",r);

70     }

71     return 0;

72 }

 

你可能感兴趣的:(poj)