您的位置:首页 > 其它

POJ 3104-Drying(二分+贪心)

2016-04-19 22:16 465 查看
C - DryingTime Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d& %I64uSubmit Status Practice POJ3104DescriptionIt 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 ata 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 amountof 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 amountof 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.InputThe 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 containsk (1 ≤ k ≤ 109).OutputOutput 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
sample output #2 
2
题意:
n件衣服各含a_i水分,自然干一分钟一单位,放烘干机一分钟k单位,一次只能晒一件。求最短时间。
思路:
这题有一个坑点,那就是用烘干机就不会自然干。而这个贪心的公式也是很容易求出来的。那就是每一件衣服一共要用烘干机多少次,设它为X次,即(mid(二分的天数)-X)+X*(一天的烘干值)〉= 当前衣服的水分就能求出答案了。
AC代码:
#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<vector>#include<map>#include<set>using namespace std;typedef long long ll;#define T 100000+50#define inf 0x3f3f3f3fll N,M;ll a[T];bool slove(ll x){ll cur = 0,j,tmp;for(int i=0;i<N;++i){if(a[i]<=x)continue;tmp = (a[i]-x)/(M-1);while((M-1)*tmp+x<a[i])tmp++;cur += tmp;if(cur>x)return false;}return true;}int main(){#ifdef zscfreopen("input.txt","r",stdin);#endifint i,j,k;while(~scanf("%lld",&N)){ll L=1,R=1000000000,mid,ans,ma=0;for(i=0;i<N;++i)scanf("%lld",&a[i]),ma=max(a[i],ma);scanf("%lld",&M);if(M==1){printf("%lld\n",ma);continue;}while(L<=R){mid = (L+R)/2;if(slove(mid)){ans = mid;R = mid-1;}else {L = mid+1;}}printf("%lld\n",ans);}return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  二分查找 贪心 poj