POJ 3104 Drying 二分

题目链接http://poj.org/problem?id=3104

题目大意:有N件衣服需要晒干,但是晒干的话是一秒蒸发一滴水,而你买个了烘干机,一秒钟可以烘干K滴水。现在给你衣服里面的含水量ai滴水以及K问最少多长时间能干。烘干机一秒只能放一件衣服。

解题思路:二分时间+check。主要问题在于check。由于烘干的时候每秒K滴水,而晒干每秒一滴水,那么我们可以认为烘干时每秒K-1滴水,任意时刻(无论在烘干与否)每秒都会自然蒸发一滴水。

那么整个过程就很清晰了:对于一个时间T,将每个ai减去T,如果ai>0,那么就计算需要多长时间烘干(K-1的烘干机),如果计算结果>T则无法完成。

注意在计算时一旦中间结果>T就返回, K = 1特判一下不然RE。。。

代码:

 1 const int inf = 0x3f3f3f3f;
 2 const int maxn = 1e5 + 5;
 3 int n, k;
 4 ll a[maxn];
 5 
 6 int check(ll x){
 7     ll ans = 0;
 8     for(int i = 1; i <= n; i++){
 9         int tmp = a[i] - x;
10         if(tmp > 0) {
11             ll u = tmp / (k - 1);
12             ll v = tmp % (k - 1);
13             if(v > 0) u++;
14             ans += u;
15         }
16         if(ans > x) return -1;
17     }
18     return 1;
19 }
20 
21 void solve(){
22     
23     ll l = 1, r = 1e9;
24     while(l < r){
25         ll mid = (l + r) >> 1;
26         if(check(mid) == -1) l = mid + 1;
27         else r = mid;
28     }
29     printf("%lld\n", l);
30 }
31 int main(){
32     scanf("%d", &n);
33     ll maxa = 0;
34     for(int i = 1; i <= n; i++) {
35         scanf("%lld", &a[i]);
36         maxa = max(a[i], maxa);
37     }
38     scanf("%d", &k);
39     if(k == 1){
40         printf("%d", maxa);
41         return 0;
42     }
43     solve();
44 }

题目:

Drying
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 17856   Accepted: 4488

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
posted @ 2017-08-16 11:30  EricJeffrey  阅读(119)  评论(0编辑  收藏  举报