poj3104 drying( 二分,最小化最大值)

题目大意就是有n件衣服, 自然风干每分钟减少1, 用洗衣机每分钟减少k, 洗衣机一次只能洗一件衣服 ,问最少需要多少时间弄干所有衣服(0)。

用二分枚举最小值, 函数C(x) 判断x 时间是否可以。 

一件衣服wat[i] 如果大于x  。 设自然干的时间为T1, 烘干为T2。 T1+T2 = X,   T1+ K*T2 >=wat[i]  可以得到枚举时间。

另外用cin可能会超时

 

 

题目:

Drying
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7575   Accepted: 1939

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

Source

Northeastern Europe 2005, Northern Subregion
 
 
代码:
poj3104 drying( 二分,最小化最大值)
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cmath>
 4 #include <queue>
 5 #include <cstdio>
 6 using namespace std;
 7 #define MAXN 100000+10
 8 #define max(x,y) x<y?y:x
 9 #define LL long long
10 LL n,k;
11 LL wat[MAXN];
12 
13 bool C(LL x)
14 {
15     LL t =0 ;
16     for(int i=0;i<n;i++)
17     {
18         if( wat[i]>x)
19         {
20             LL xx = ceil( (wat[i]-x)*1.0/(k-1));
21             t+=xx;
22         }
23         if(t>x)return false;
24     }
25 
26     if(t>x)return false;
27     else
28         return true;
29 }
30 
31 
32 int main()
33 {
34     ios::sync_with_stdio(false);
35     while(scanf("%lld",&n)!=EOF)
36     {
37         LL l=1, r=0;
38         for(int i=0;i<n;i++)
39         {
40             scanf("%lld",&wat[i]);
41             r= max( r, wat[i]);
42         }
43         scanf("%lld",&k);
44 
45         if( k == 1)
46         {
47             printf("%lld\n",r);
48             continue;
49         }
50         LL mid=0;
51         LL ans=0;
52         while(l<=r)
53         {
54             mid = (l+r)/2;
55             //cout<<mid<<endl;
56             if( C(mid) )
57             {
58                 r = mid-1;
59                 ans = mid;
60             }
61             else
62                 l = mid+1;
63         }
64         printf("%lld\n",ans);
65     }
66     return 0;
67 }
poj3104 drying( 二分,最小化最大值)

 

poj3104 drying( 二分,最小化最大值)

上一篇:归并排序非递归算法


下一篇:Ubuntu的备份和还原