poj 3104 Drying(二分搜索之最大化最小值)
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
晾衣服: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(二分搜索之最大化最小值)的更多相关文章
- poj 3104 晾衣服问题 最大化最小值
题意:n件衣服各含有ai水分,自然干一分钟一个单位,放烘干机一分钟k个单位,问:最短时间? 思路: mid为最短时间 如果 a[i]-mid>0说明需要放入烘干机去烘干 烘干的时间为x 那么满 ...
- POJ 3104 Drying(二分答案)
题目链接:http://poj.org/problem?id=3104 ...
- hihocoder 二分·二分答案【二分搜索,最大化最小值】 (bfs)
题目 这道题做了几个小时了都没有做出来,首先是题意搞了半天都没有弄懂,难道真的是因为我不打游戏所以连题都读不懂了? 反正今天是弄不懂了,过几天再来看看... 题意:一个人从1点出发到T点去打boss, ...
- poj 3273 Monthly Expense(二分搜索之最大化最小值)
Description Farmer John ≤ moneyi ≤ ,) that he will need to spend each day over the next N ( ≤ N ≤ ,) ...
- poj 2456 Aggressive cows(二分搜索之最大化最小值)
Description Farmer John has built a <= N <= ,) stalls. The stalls are located along a straight ...
- poj 3258 River Hopscotch(二分搜索之最大化最小值)
Description Every year the cows hold an ≤ L ≤ ,,,). Along the river between the starting and ending ...
- POJ 3104 Drying 二分
http://poj.org/problem?id=3104 题目大意: 有n件衣服,每件有ai的水,自然风干每分钟少1,而烘干每分钟少k.求所有弄干的最短时间. 思路: 注意烘干时候没有自然风干. ...
- poj 3104 Drying(二分查找)
题目链接:http://poj.org/problem?id=3104 Drying Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
- POJ 3104 Drying(二分答案)
[题目链接] http://poj.org/problem?id=3104 [题目大意] 给出n件需要干燥的衣服,烘干机能够每秒干燥k水分, 不在烘干的衣服本身每秒能干燥1水分 求出最少需要干燥的时间 ...
随机推荐
- Wamp集成环境安装
一.Wamp下载 点我下载WampServer2.1a-x32 二.Wamp安装步骤 三.修改语言为汉语 四.查看测试页面
- 把本地建好的项目提交到git上
才开始用git来控制项目版本,刚开始时不是很会用,由于公司最近新开个项目,需要我把建好的项目放到git上去,慢慢的摸索,终于有点小小的结果,把一个项目成功提交到git上了,在这里记录下,以免下次忘记, ...
- 金典 SQL笔记(4)
由于在本地笔记上写的.CSDN markdown编辑器仅仅支持.md格式导入, 图片没办法直接导进去.写的多了懒的一张一张图片切图上传; 直接整个文章切成图片上传上去了. watermark/2/te ...
- IntelliJ IDEA 的Project structure说明
IntelliJ IDEA 的Project structure可以在File->Project structure中打开,同时,在新建项目是IDE一般用向导的方式让你填写Project str ...
- javaScript模块化一
1. Module模式的基本特性 A) 模块化 可重用 B) 封装了变量和function 和全局的namespace不接触 松耦合. C) 只暴露可用public的方法 其他私有方法全部隐藏 ...
- C#之out与ref的共性与区别以及用法
引入: 首先看一个例子: class Program { static void Main(string[] args) { ; int result = Test(number); Console. ...
- python 下的数据结构与算法---5:递归(Recursion)
定义:递归就是不断分割整体成部分直到可以轻易解决分割出来的部分. 递归表达式三定律: 1:递归表达式必须有个最小单元 (最小单元既是停止递归调用以及能够直接运算的) 2:递归表达式在运算过程中 ...
- 用ueditor上传图片、文件等到七牛云存储
ueditor上传文件,是用数据流的形式上传的. 而七牛云存储官方文档中,只提供了文件路径上传的方式. 但是,仅仅是在官方文档中写了这一种方式. 事实上,利用VS的对象管理器,打开Qiniu的dll, ...
- Xcode itunes完美打包api方法
转:http://bbs.csdn.net/topics/390948190 Xcode6 itunes完美打包api 方法! 特点轻盈小巧,方便快捷!
- apt-get install jdk
怕忘记,记录下: sudo apt-get install python-software-properties sudo add-apt-repository ppa:webupd8team/jav ...