博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Drying POJ - 3104(二分+思维)
阅读量:4135 次
发布时间:2019-05-25

本文共 2661 字,大约阅读时间需要 8 分钟。

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
题意:晾衣服,每分钟可以晾干1单位的水。用烘干机的话可以每分钟烘干k单位的水,但是一次只能烘干一件(烘干的时候忽略风干的影响),问最小多少分钟可以烘干所有的衣服。
思想:对于一件衣服,完全风干需要a[i]的时间。我们二分去枚举一个答案x,对于水分小于等于x的值,我们就不用管了。对于大于x的衣服,可以分为两部分,一部分晾衣服,一部分烘干衣服。那么一定存在一个值m,使得烘干的水量为(m-c)+ck的和最小。其中m为烘干衣服所用的时间,c为烘干的时间。也就是说现在要求的是 a[i]=(m-c)+ck -> c=(a[i]-m)*1.0/(k-1);我们求出所有衣服烘干时间的总和,判断是否大于答案x就行。详细解释看代码
代码如下:

#include
#include
#include
#define ll long longusing namespace std;const int maxx=1e5+100;ll a[maxx],b[maxx],k;int n;bool judge(ll x)//x为烘干所有衣服的时间,y为这件衣服使用烘干机的时间。那么(x-y)+y*k=a[i]。那么使用烘干机的时间为y=(a[i]-x)/(k-1)。求出所有衣服使用烘干机的时间,不要大于x就好了。 {
for(int i=1;i<=n;i++) b[i]=a[i]-x; ll cnt=0; for(int i=1;i<=n;i++) {
if(b[i]>0) {
cnt+=b[i]%(k-1)?b[i]/(k-1)+1:b[i]/(k-1); } } if(cnt<=x) return 1; return 0;}int main(){
while(~scanf("%d",&n)) {
for(int i=1;i<=n;i++) scanf("%lld",&a[i]); scanf("%lld",&k); sort(a+1,a+1+n); if(k==1) {
printf("%lld\n",a[n]); continue; } ll l=1,r=a[n],mid,ans=0; while(l<=r) {
mid=l+r>>1; if(judge(mid)) {
ans=mid; r=mid-1; } else l=mid+1; } printf("%lld\n",ans); } return 0;}

努力加油a啊,(o)/~

转载地址:http://wktvi.baihongyu.com/

你可能感兴趣的文章