题目描述:
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件衣服要烘干,第i件衣服的水分为a[i],烘干机每次只能放进一件衣服,每分钟能烘干的水分为k,同时每分钟没有放在洗衣机的衣服的水分会自然蒸发1,求最短需要多少时间把衣服全部烘干。
题解:
二分。
设给i件衣服用了t时间的洗衣机,那么mid时间后,自然风干后剩下的水就是 a[i]-(mid-t) (因为在洗衣机里不能风干)。
剩下的问题就是如何得到这个t,很简单,风干后剩下的水就得用洗衣机来除掉,又知道用了t时间的洗衣机。我们可以推导出一个式子:k*t=a[i]-(mid-t) 我们需要看的就是这些t的总和于mid的比较关系
移项之后可以得到:t=(a[i]-mid)/(k-1)
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn = 100000 + 5;
typedef long long ll;
ll a[maxn],k;
int n;
int bsearch(ll x){
ll sum = 0;
for(int i = 0; i < n; i ++){
if(a[i] > x){
sum += (a[i] -x) / (k - 1);
if((a[i] - x) % (k - 1) != 0) sum ++;
}
}
if(sum > x) return 0;
else return 1;
}
int main(){
while(scanf("%d",&n)!=EOF){
for(int i = 0; i < n; i ++) scanf("%lld",&a[i]);
scanf("%lld",&k);
sort(a,a + n);
ll l = 1;
ll r = a[n - 1];
if(k == 1){
printf("%lld\n",r);
}
else{
while(l <= r){
//cout<<l<<" "<<r<<endl;
ll mid = (l + r) / 2;
if(bsearch(mid) == 1) r = mid - 1;
else l = mid + 1;
}
printf("%lld\n",l);
}
}
return 0;
}