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

The first line contains a single integer n ( ≤ n ≤  ). The second line contains ai separated by spaces ( ≤ ai ≤ ). The third line contains k ( ≤ k ≤ ).

Output

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

Sample Input

sample input #

sample input #

Sample Output

sample output #

sample output #

Source

Northeastern Europe 2005, Northern Subregion
 

晾衣服:n件衣服各含a_i水分,自然干一分钟一单位,放烘干机一分钟k单位,一次只能晒一件。求最短时间。

取C(mid) := 能在mid分钟内处理完,然后二分即可。

这里有两个很好玩的陷阱

①每分钟烘干k单位的水,于是我就想当然地除k向上取整了((a_i – mid) / k)。其实应该除以k-1,列个详细的算式:

设需要用x分钟的机器,那么自然风干需要mid – x分钟,x和mid需要满足:

k*x + (mid – x) >= a_i,即 x >= (a_i – mid) / (k – 1)。

②当k=1的时候,很显然会发生除零错误,需要特殊处理。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
using namespace std;
#define N 100006
#define ll long long
ll n;
ll k;
ll a[N];
bool solve(ll mid){
ll minute=;
for(ll i=;i<n;i++){
if(a[i]>mid){
minute+=(ceil((a[i]-mid)*1.0/(k-)));
}
}
if(minute>mid) return false;
return true; }
int main()
{
int ac=;
while(scanf("%I64d",&n)==){ ll low=;
ll high=;
for(ll i=;i<n;i++){
scanf("%I64d",&a[i]);
high=max(high,a[i]);
}
scanf("%I64d",&k);
if(k==){
printf("%I64d\n",high);
continue;
}
ll ans;
while(low<high){ ll mid=(low+high)>>;
if(solve(mid)){
high=mid;
}
else{
low=mid+; }
} printf("%I64d\n",low);
}
return ;
}

poj 3104 Drying(二分搜索之最大化最小值)的更多相关文章

  1. poj 3104 晾衣服问题 最大化最小值

    题意:n件衣服各含有ai水分,自然干一分钟一个单位,放烘干机一分钟k个单位,问:最短时间? 思路: mid为最短时间 如果 a[i]-mid>0说明需要放入烘干机去烘干 烘干的时间为x  那么满 ...

  2. POJ 3104 Drying(二分答案)

    题目链接:http://poj.org/problem?id=3104                                                                  ...

  3. hihocoder 二分·二分答案【二分搜索,最大化最小值】 (bfs)

    题目 这道题做了几个小时了都没有做出来,首先是题意搞了半天都没有弄懂,难道真的是因为我不打游戏所以连题都读不懂了? 反正今天是弄不懂了,过几天再来看看... 题意:一个人从1点出发到T点去打boss, ...

  4. poj 3273 Monthly Expense(二分搜索之最大化最小值)

    Description Farmer John ≤ moneyi ≤ ,) that he will need to spend each day over the next N ( ≤ N ≤ ,) ...

  5. poj 2456 Aggressive cows(二分搜索之最大化最小值)

    Description Farmer John has built a <= N <= ,) stalls. The stalls are located along a straight ...

  6. poj 3258 River Hopscotch(二分搜索之最大化最小值)

    Description Every year the cows hold an ≤ L ≤ ,,,). Along the river between the starting and ending ...

  7. POJ 3104 Drying 二分

    http://poj.org/problem?id=3104 题目大意: 有n件衣服,每件有ai的水,自然风干每分钟少1,而烘干每分钟少k.求所有弄干的最短时间. 思路: 注意烘干时候没有自然风干. ...

  8. poj 3104 Drying(二分查找)

    题目链接:http://poj.org/problem?id=3104 Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissio ...

  9. POJ 3104 Drying(二分答案)

    [题目链接] http://poj.org/problem?id=3104 [题目大意] 给出n件需要干燥的衣服,烘干机能够每秒干燥k水分, 不在烘干的衣服本身每秒能干燥1水分 求出最少需要干燥的时间 ...

随机推荐

  1. JS 中的引用

    首先有一个全局变量  JsonArry={"key":"value"}; 假设这个object为{"你好":"引用"} ...

  2. 1034 - Navigation

    Global Positioning System (GPS) is a navigation system based on a set of satellites orbiting approxi ...

  3. [Javascript] Safe Nested Object Inspection

    A common problem when dealing with some kinds of data is that not every object has the same nested s ...

  4. exit和abort都是用来终止程序的函数

    exit会做一些释放工作:释放所有的静态的全局的对象,缓存,关掉所有的I/O通道,然后终止程序.如果有函数通过atexit来注册,还会调用注册的函数.不过,如果atexit函数扔出异常的话,就会直接调 ...

  5. DFBle.swift

    ////  DFBle.swift//  DFBle////  Created by LeeYaping on 15/9/2.//  Copyright (c) 2015年 lisper. All r ...

  6. boost::pool与内存池技术

      建议看这个链接的内容:http://cpp.winxgui.com/cn:mempool-example-boost-pool Pool分配是一种分配内存方法,用于快速分配同样大小的内存块,    ...

  7. JAVA--聊天界面面板

    package windows.beautify; import java.awt.BorderLayout; import java.awt.Color; import java.awt.event ...

  8. datagrid的基本操作-增删改

    1 ---恢复内容开始--- <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  9. 小技巧之jQueryMobile

    使用JqueryMobile+MVC做一个手机网站,也有2个月了.有一些小小的经验,跟大伙们分享一下下 小技巧1: 禁用所有Ajax加载,它会很烦人的. $.mobile.ajaxLinksEnabl ...

  10. jquery获取checkbox被选中的值

    只用一个循环,就可以找出被选中的checkbox的值 var s; $("[name = b]:checkbox").each(function () {              ...