首页 > 代码库 > POJ 3104 Drying [二分 有坑点 好题]

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 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 #132 3 95sample input #232 3 65

Sample Output

sample output #13sample output #22

Source

Northeastern Europe 2005, Northern Subregion

 

本来想贪心的,结果越写越乱,老老实实二分。。。不过这题有其精妙之处,wa的人先看样例

 

送点test data给WA的朋友们……

Posted by MIRKING at 2008-07-31 13:39:06 on Problem 3104 and last updated at 2008-07-31 13:40:02


test data:2892result:6710000000001000000000100000000010000000001000000000100000000010000000002result:875000000

















Re:为什么二分时 枚举的时间l,r,mid不用__int64就wa了

Posted by wangyaoxuan at 2010-10-14 11:30:23 on Problem 3104 
In Reply To:为什么二分时 枚举的时间l,r,mid不用__int64就wa了 Posted by:wangyaoxuan at 2010-10-14 11:22:39

表示理解了,因为过程中求得的时间值t会超过int,后来的wa们慢慢体会....

 

Posted by lgq1205 at 2009-12-03 12:54:33 on Problem 3104


二分枚举时间,判断可行性,然后求解即可.注意判断可行性的过程:1.先把所有衣服的含水量减去T2.然后把>=(k-1)的拿去烘干,可以理解为烘干时候每分钟掉(k-1)水,这样所有的衣服都每分钟自然干掉1水了。因为每分钟掉一滴水是肯定的了,因此,如果你去烘干它的话,那么它就能再掉多k-1 + 1 == k,这样才是k滴水,然后就是计算总花费时间3.最后判断总花费时间时候小于T,若小于等于,则可行性,否则不可行.

如果你WA了,看一下是否有变量要设为64位int,如果RE了,看一下除零。。。

Posted by 2745972075 at 2014-07-21 15:47:54 on Problem 3104


 

 1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<map> 9 #include<set>10 #include<stack>11 #include<string>12 13 #define N 10000514 #define M 1015 #define mod 100000000716 //#define p 1000000717 #define mod2 100000000018 #define ll long long19 #define LL long long20 #define eps 1e-921 //#define inf 0x3fffffffff22 #define maxi(a,b) (a)>(b)? (a) : (b)23 #define mini(a,b) (a)<(b)? (a) : (b)24 25 using namespace std;26 27 int n;28 ll ans;29 ll a[N];30 ll k;31 32 void ini()33 {34     ans=0;35     int i;36     for(i=1;i<=n;i++){37         scanf("%I64d",&a[i]);38     }39     scanf("%I64d",&k);40     sort(a+1,a+1+n);41 }42 43 int ok(ll m)44 {45     int i;46     ll le=m;47     ll re;48     for(i=1;i<=n;i++){49         re=a[i]-m;50         if(re<=0) continue;51         le-=(re+k-2)/(k-1);52     }53     if(le>=0) return 1;54     return 0;55 }56 57 void solve()58 {59     ll l=1;60     ll r=a[n];61     ll m;62     //printf(" %d\n",a[1]);63     if(k==1){64         ans=a[n];return;65     }66     while(l<r)67     {68         m=l+(r-l)/2;69         if(ok(m)==1){70             r=m;71         }72         else{73             l=m+1;74         }75     }76     ans=l;77 }78 79 void out()80 {81     printf("%I64d\n",ans);82 }83 84 int main()85 {86    // freopen("data.in","r",stdin);87     //freopen("data.out","w",stdout);88     //scanf("%d",&T);89     //for(int ccnt=1;ccnt<=T;ccnt++)90     //while(T--)91     while(scanf("%d",&n)!=EOF)92     {93         ini();94         solve();95         out();96     }97 98     return 0;99 }

 

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