题目链接:drying
题目:
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
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.
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 a single integer — the minimal possible number of minutes required to dry all clothes.
sample input #1 3 2 3 9 5 sample input #2 3 2 3 6 5
sample output #1 3 sample output #2 2
题目大意:N件衣服,每件都有自己的水分量,水分每分钟自然减少一点,一个烘干机能够每分钟烘去k点水分。但是他每次只能作用在一件 衣服上。比较坑的一点是在用烘干机烘一件衣服时是不会自然减少水分的。问你最少需要多久能全部烘干。
思路:虽然是在做二分专题,但是刚开始想都没想就觉得应该每次取水分含量最多的去烘,然后果断用了优先队列,超时了......10^9的衣服数量不是盖的。然后又只能二分答案咯,最关键的地方就是判断二分的时候的答案的合法性。设X为需要用烘干机的时间,t为总共用去的时间。则有X*K+(t-X)>=w[i]--------X>=(w[i]-t)/(K-1).根据这个式子就可以求出要烘干一件衣服最少需要对他使用烘干机的次数。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
long long w[111111];
long long n,k,maxw,ans;
int judge(long long x)
{
long long sum=0;
for(int i=0;i<n;i++)
{
if(w[i]>x)
{
sum+=(w[i]-x)/(k-1);
if((w[i]-x)%(k-1))
sum++;
}
}
if(sum<=x)
return 1;
else
return 0;
}
void bs(long long l,long long r)
{
long long mid;
while(l<=r)
{
mid=(r-l)/2+l;
if(judge(mid))
{
ans=min(ans,mid);
r=mid-1;
}
else
l=mid+1;
}
}
int main()
{
while(scanf("%lld",&n)!=EOF)
{
maxw=-1;
ans=0x3f3f3f3f;
for(int i=0;i<n;i++)
{
scanf("%lld",&w[i]);
maxw=max(maxw,w[i]);
}
scanf("%lld",&k);
if(k==1)
{
printf("%d\n",maxw);
continue;
}
sort(w,w+n);
bs(1,maxw);
printf("%lld\n",ans);
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
long long w[111111];
long long n,k,maxw,ans;
int judge(long long x)
{
long long sum=0;
for(int i=0;i<n;i++)
{
if(w[i]>x)
{
sum+=(w[i]-x)/(k-1);
if((w[i]-x)%(k-1))
sum++;
}
}
if(sum<=x)
return 1;
else
return 0;
}
void bs(long long l,long long r)
{
long long mid;
while(l<=r)
{
mid=(r-l)/2+l;
if(judge(mid))
{
ans=min(ans,mid);
r=mid-1;
}
else
l=mid+1;
}
}
int main()
{
while(scanf("%lld",&n)!=EOF)
{
maxw=-1;
ans=0x3f3f3f3f;
for(int i=0;i<n;i++)
{
scanf("%lld",&w[i]);
maxw=max(maxw,w[i]);
}
scanf("%lld",&k);
if(k==1)
{
printf("%d\n",maxw);
continue;
}
sort(w,w+n);
bs(1,maxw);
printf("%lld\n",ans);
}
return 0;
}