Drying

Drying

http://poj.org/problem?id=3104

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 23212   Accepted: 5842

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

 

设x1为自然晾干的时间,x2为被烘干的时间

a_i=x1+k*x2,mid=x1+x2,可得 x2=(a_i-mid)/(k-1),答案向上取整

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<string>
 4 #include<map>
 5 #include<vector>
 6 #include<cmath>
 7 #include<string.h>
 8 #include<stdlib.h>
 9 #include<stack>
10 #include<queue>
11 #include<cstdio>
12 #include<vector>
13 typedef long long ll;
14 const long long MOD=1000000007;
15 #define maxn 100005
16 using namespace std;
17 
18 ll n,k;
19 ll a[maxn];
20 
21 bool Check(ll mid){
22     ll sum=0;
23     ll tmp,tt;
24     for(int i=1;i<=n;i++){
25         if(a[i]>mid){
26             sum+=ll(ceil((a[i]-mid)*1.0/(k-1)));
27         }
28     }
29     if(sum<=mid) return true;
30     return false;
31 }
32 
33 int main(){
34     while(~scanf("%lld",&n)){
35         ll L=0,R=0,mid;
36         for(int i=1;i<=n;i++){
37             scanf("%lld",&a[i]);
38             R=max(R,a[i]);
39         }
40         scanf("%lld",&k);
41         if(k==1){
42             cout<<R<<endl;
43             continue;
44         }
45         
46         while(L<=R){
47             mid=L+R>>1;
48             if(Check(mid)){
49                 R=mid-1;
50             }
51             else{
52                 L=mid+1;
53             }
54         }
55         printf("%lld\n",L);
56     }
57 }
View Code

 

posted on 2018-12-05 13:23  Fighting_sh  阅读(496)  评论(0编辑  收藏  举报

导航