题目:
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
题意:
给你一个数字N,代表有N件衣服;下面的N个数字代表每一件衣服带有多少单位的水;
给你一个数字K,代表使用烘干机每一分钟失去K单位的水;
衣服会自然晾干,每一分钟会失去一单位的水分;
问你把这些衣服全部烘干需要多久的时间?
思路:
对于这一道题可以直接使用二分搜索来解决,直接对于解决的时间来进行二分判断枚举,找到最小的时间即可;
需要注意的是,数据的输入和输出,没有数字之外的输入和输出,否则会错误;
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
这些数据相当于:
Sample Input
3
2 3 9
5
3
2 3 6
5
Sample Output
3
2
乱七八糟的东西全不要了,感觉题目描述不清楚;
注意:对于k=1时,数据需要单独判断,否则还是会错误;
代码如下:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=100010;
ll a[N];
ll n,k;
ll low,high,mid;
int judge(ll x)//在x分钟内要解决问题;
{ //解决了就返回1,否则返回0;
int sum=0;
for(int i=0; i<n; i++)
{
if(a[i]>x)//无法自然风干,需要用烘干机;
sum+=(a[i]-x+k-2)/(k-1);
if(sum>x)//超出给出的时间,不能解决这些衣服;
return 0;
}
return 1;
}
void solve()
{
low=1;
high=a[n-1];
while(low<=high)
{
mid=(low+high)/2;
if(judge(mid))//给出的时间内能够解决问题,那么就缩短时间;
high=mid-1;
else//不能解决问题,那么久扩大时间;
low=mid+1;
}
printf("%lld\n",low);
return ;
}
int main()
{
while(~scanf("%lld",&n))
{
for(int i=0; i<n; i++)
scanf("%lld",&a[i]);
scanf("%lld",&k);
sort(a,a+n);
//printf("sample output #%d\n",++tt);
//不要上面的输出,否则就错了,题目描述的不清楚,需要注意了;
if(k==1)//要单独判断,否则会出错;
printf("%lld\n",a[n-1]);
else
solve();
}
return 0;
}