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. fetch策略

    @OneToMany(mappedBy="image",cascade=CascadeType.ALL,fetch=FetchType.EAGER) @Fetch(value=Fe ...

  2. POJ burnside&&polya整理练习

    POJ 2409 Let it Bead 这题就是polya公式的直接套用,唯一麻烦的是置换群的种类数,由于可以翻转,所以除了要加上pow(c,gcd(s,i))这些平面旋转的置换群,还要加上翻转的. ...

  3. [React] React Router: Named Components

    In this lesson we'll learn how to render multiple component children from a single route. Define a n ...

  4. sql中的case when

    sql语言中有没有类似C语言中的switch case的语句?? 没有,用case   when   来代替就行了.            例如,下面的语句显示中文年月         select ...

  5. 如何使用easyUI

    一.简介 以下内容来自百度: jQuery EasyUI是一组基于jQuery的UI插件集合,而jQuery EasyUI的 目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面.开发者不 ...

  6. Linux的VI/VIM

    参考自:http://www.cnblogs.com/itech/archive/2009/04/17/1438439.html 作者:iTech 出处:http://itech.cnblogs.co ...

  7. 3.2:pandas数据的导入与导出【CSV,JSON】

    一:CSV数据 一]:导入数据 1)从CSV文件读入数据:pd.read_csv("文件名"),默认以逗号为分隔符 D:\data\ex1.csv文件内容:             ...

  8. Lambda表达式 - 浅谈

    概述: 只要有委托参数类型的地方,就可以使用 Lambda表达式.在讲述Lambda表达式之前,有必要先简要说明一下 委托中的"匿名方法": using System; using ...

  9. linux下面安装配置LAMP环境

    以centos下面为例.初学者.东西基本都是各个地方找来的.自己手写了一遍.应该印象会很深刻 首先切换到超级管理员模式 1.安装php 一路选择y就行了 安装一些php的扩展 yum -y insta ...

  10. zepto自定义事件

    <!--测试自定义事件 trigger--> <script> $(function () { var meEvent = $.Event("custom" ...