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

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 }

时间: 2024-08-07 08:27:45

poj 3104 Drying(二分搜索之最大化最小值)的相关文章

poj 3104 Drying (二分)

/*设某次二分出的一个值是mid: 1.对于一件ai值小于等于mid的衣服,直接晾干即可: 2.对于一件ai值大于mid值的衣服,最少的用时是用机器一段时间, 晾干一段时间,设这两段时间分别是x1和x2, 那么有mid=x1+x2,ai<=k*x1+x2,解得x1>=(ai-mid)/(k-1) , 所以对(ai-mid)/(k-1)向上取整就是该件衣服的最少用时.*/ # include <stdio.h> # include <string.h> # include

POJ 3104 Drying(二分答案)

题目链接:http://poj.org/problem?id=3104 Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11128   Accepted: 2865 Description It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She is not afra

【贪心专题】POJ 3258 River Hopscotch (最大化最小值 贪心+二分搜索)

链接:click here~~ [题意] 一条河长度为 L,河的起点(Start)和终点(End)分别有2块石头,S到E的距离就是L,河中有n块石头,每块石头到S都有唯一的距离,,现在要你移除其中的m块,使得具有最小间距的相邻两块石头之间的距离最大. [解题思路] 又是一道经典的二分搜索,跟前一道一样的思路,不过要注意的是:此题是移除其中的元素,从而达到最大化的最小值. 代码: #include <stdio.h> #include <string.h> #include <

poj 3273 Monthly Expense(二分搜索之最大化最小值)

Description Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money (1 ≤ moneyi ≤ 10,000) that he will need to spend each day over the

poj 2456 Aggressive cows(二分搜索之最大化最小值)

Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000). His C (2 <= C <= N) cows don't like this barn layout an

POJ 3104 Drying

Drying Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 310464-bit integer IO format: %lld      Java class name: Main It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart g

POJ 3104 Drying [二分 有坑点 好题]

传送门 表示又是神题一道 Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9327   Accepted: 2364 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 proces

POJ - 3258 River Hopscotch(二分 最大化最小值)

大致题意: 坐标 从  [0-L], 上有 N 个点 , 现在需要移去 M 个点.求任意两点(包括0,L)点的距离中最小值的最大值 思路:枚举答案,若移去M个点不能达成目标,则使答案变小. while(l <= r){ ll mid = l + (r-l)/2; // 是否可能使得所有石头之间的距离不小于 mid if(check(mid)){ l = mid+1; }else{ r = mid-1; } } printf("%lld\n",l-1); 1 bool check(

POJ 3104 Drying(二分)

题目描述: Drying Time Limit: 2000MS Memory Limit: 65536K 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. B