poj 3104 Drying(二分查找)

来源:互联网 发布:淘宝男士鞋子店铺 编辑:程序博客网 时间:2024/04/29 18:38
题目链接:http://poj.org/problem?id=3104
                                       Drying
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 9440 Accepted: 2407

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件衣服,每件衣服有一定的水量,使衣服干有两种方法:1、水分蒸发,每分钟蒸发掉一滴水 2、用烘干机烘干,每分钟烘干掉k滴水。题目要输出的是是所有衣服变干的最小时间。

详见代码。
 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4  5 using namespace std; 6  7 #define ll long long 8  9 int main ()10 {11     int n;12     ll a[100000+10];13     ll tmax;14     while (~scanf("%d",&n))15     {16         tmax=0;17         for (int i=0; i<n; i++)18         {19             scanf("%lld",&a[i]);20             if (a[i]>tmax)21                 tmax=a[i];22         }23         ll k;24         scanf("%lld",&k);25         if (k==1)26         {27             printf ("%lld",tmax);28             continue;29         }30         ll l=1,r=tmax,mid,ans=0;31         while (r>=l)32         {33             ll sum=0;34             mid=(l+r)/2;35             for (int i=0;i<n;i++)36             {37                 if(a[i]>mid)38                 {39                     ll s=ceil((a[i]-mid)*1.0/(k-1));40                     sum+=s;41                 }42             }43             if (sum>mid)44                 l=mid+1;45             else46                 r=mid-1,ans=mid;47         }48         printf ("%lld\n",ans);49     }50     return 0;51 }

 

 
0 0
原创粉丝点击