码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3104 Drying(二分搜索之最大化最小值)

时间:2015-09-03 20:22:54      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

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
 

晾衣服:n件衣服各含a_i水分,自然干一分钟一单位,放烘干机一分钟k单位,一次只能晒一件。求最短时间。

取C(mid) := 能在mid分钟内处理完,然后二分即可。

这里有两个很好玩的陷阱

①每分钟烘干k单位的水,于是我就想当然地除k向上取整了((a_i – mid) / k)。其实应该除以k-1,列个详细的算式:

设需要用x分钟的机器,那么自然风干需要mid – x分钟,x和mid需要满足:

k*x + (mid – x) >= a_i,即 x >= (a_i – mid) / (k – 1)。

②当k=1的时候,很显然会发生除零错误,需要特殊处理。

 

技术分享
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<stdlib.h>
 7 using namespace std;
 8 #define N 100006
 9 #define ll long long
10 ll n;
11 ll k;
12 ll a[N];
13 bool solve(ll mid){
14     ll minute=0;
15     for(ll i=0;i<n;i++){
16         if(a[i]>mid){
17             minute+=(ceil((a[i]-mid)*1.0/(k-1)));
18         }
19     }
20     if(minute>mid) return false;
21     return true;
22 
23 }
24 int main()
25 {
26     int ac=0;
27     while(scanf("%I64d",&n)==1){
28 
29         ll low=1;
30         ll high=0;
31         for(ll i=0;i<n;i++){
32             scanf("%I64d",&a[i]);
33             high=max(high,a[i]);
34         }
35         scanf("%I64d",&k);
36         if(k==1){
37             printf("%I64d\n",high);
38             continue;
39         }
40         ll ans;
41         while(low<high){
42 
43             ll mid=(low+high)>>1;
44             if(solve(mid)){
45                 high=mid;
46             }
47             else{
48                 low=mid+1;
49                 
50             }
51         }
52 
53         printf("%I64d\n",low);
54     }
55     return 0;
56 }
View Code

 

poj 3104 Drying(二分搜索之最大化最小值)

标签:

原文地址:http://www.cnblogs.com/UniqueColor/p/4780612.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!