博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3104 Drying(二分搜索之最大化最小值)
阅读量:6306 次
发布时间:2019-06-22

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

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 (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 #132 3 95sample input #232 3 65

 

Sample Output

sample output #13sample output #22

 

Source

, 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的时候,很显然会发生除零错误,需要特殊处理。

 

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std; 8 #define N 100006 9 #define ll long long10 ll n;11 ll k;12 ll a[N];13 bool solve(ll mid){14 ll minute=0;15 for(ll i=0;i
mid){17 minute+=(ceil((a[i]-mid)*1.0/(k-1)));18 }19 }20 if(minute>mid) return false;21 return true;22 23 }24 int main()25 {26 int ac=0;27 while(scanf("%I64d",&n)==1){28 29 ll low=1;30 ll high=0;31 for(ll i=0;i
>1;44 if(solve(mid)){45 high=mid;46 }47 else{48 low=mid+1;49 50 }51 }52 53 printf("%I64d\n",low);54 }55 return 0;56 }
View Code

 

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

你可能感兴趣的文章
【转】阿里面试经历及总结(数据研发、Java研发方向)
查看>>
JAVA NIO 网络阻塞IO与非阻塞IO
查看>>
Linux-查找命令的使用情况(持续更新)
查看>>
How to install webmin on ubuntu 12.04 (Precise)...
查看>>
Python 读取文件示例[一]
查看>>
第24讲 js案例讲解 js自定义函数
查看>>
SFB 项目经验-33-分配公网证书 For 负载均衡-Keepalived-Haproxy
查看>>
jquery mobile左右滑动切换页面
查看>>
[每日一题] OCP1z0-047 :2013-08-11 描述层次查询(hierarchical query)........................31...
查看>>
Shell命令:echo 命令详解
查看>>
尼姆博弈
查看>>
【推荐】程序员必读的三十本经典巨作
查看>>
DES函数加密算法
查看>>
我的友情链接
查看>>
SEO工作之友好引导(二)
查看>>
ifcfg/ip/ss命令详解
查看>>
关于 Flume NG
查看>>
北电交换机常用配置
查看>>
Linux磁盘及文件系统管理
查看>>
Linux系统下Apache日志文件设置、更改默认网站目录、防止php***跨站设置、禁止空主机头...
查看>>