0
点赞
收藏
分享

微信扫一扫

Drying (二分)

朱小落 2022-08-10 阅读 64



Problem 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

<span lang="en-us"><p>The first line contains a single integer <i>n</i> (1 ≤ <i>n</i> ≤ 100 000). The second line contains <i>a<sub>i</sub></i> separated by spaces (1 ≤ <i>a<sub>i</sub></i> ≤ 10<sup>9</sup>). The third line contains <i>k</i> (1 ≤ <i>k</i> ≤ 10<sup>9</sup>).</p></span>

Output

<p>Output a single integer — the minimal possible number of minutes required to dry all clothes.</p>


Sample Input

<b>sample input #1</b>
3
2 3 9
5

<b>sample input #2</b>
3
2 3 6
5


Sample Output

<b>sample output #1</b>
3

<b>sample output #2</b>
2



题目大概:

一道关于把衣服晾干的题,这道题中衣服一分钟掉一单位的水,吹风机一分钟掉k单位的水,给出衣服的湿度,问最少几分钟。

思路:

需要注意的是,这k单位包含自然风干的一单位的水,这是最坑爹的,在这里很容易出错。然后,自然是二分时间,然后看看是否能够晾干,再分情况赋值,值得一提的是这里要向上取整。在整数运算中a/b的向上取整为(a+b-1)/b。

代码:


#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
long long n;
long long a[100005];
long long k;

int pan(long long mid)
{ long long sun=0;

for(long long i=1;i<=n;i++)
{
if(a[i]>mid)
{
sun+=(a[i]-mid+k-2)/(k-1);
if(sun>mid)return 0;
}

}

return 1;



}

int main()
{
while(scanf("%I64d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
{
scanf("%I64d",&a[i]);

}
scanf("%I64d",&k);
sort(a+1,a+1+n);
if(k==1)
{printf("%I64d\n",a[n]);
continue;
}
long long l,r,mid;
l=1;
r=a[n];
int aa;
while(l<=r)
{
mid=(l+r)/2;
if(pan(mid))
{ aa=mid;
r=mid-1;
}
else
{
l=mid+1;
}

}


printf("%d\n",aa);
}
return 0;
}



举报

相关推荐

0 条评论