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水分 求出最少需要干燥的时间 ...
随机推荐
- (转)Eclipse/Myeclipse 注释注释模板
Window -->preferences --> Java --> Code Style --> Code Templates --> Comments --> ...
- cpio.gz 解压
linux下cpio.gz文件的解压方法:今天下载了 10201_database_linux_x86_64.cpio.gz 文件,解压方法如下:1. gunzip 10201_database_li ...
- Linux命令之用户与组管理
介绍 Linux操作系统中,任何文件都归属某一特定的用户,而任何用户都隶属至少一个用户组.用户是否有权限对某文件进行访问.读写以及执行,受到系统严格约束的正式这种清晰.严谨的用户与用户组管理系统.在很 ...
- SQL Profiler工具简介
一.SQL Profiler工具简介 SQL Profiler是一个图形界面和一组系统存储过程,其作用如下: 图形化监视SQL Server查询: 在后台收集查询信息: 分析性能: 诊断像死锁之类的问 ...
- html5.边框属性相关知识点
border-left 定义左边框 border-top 定义上边框 border-right 定义有边框 border-bottom 定义下边框 边框样式: dotted 边框线为点状虚线 dash ...
- 自动生成 Lambda查询和排序,从些查询列表so easy
如下图查询页面,跟据不同条件动态生成lambda的Where条件和OrderBy,如果要增加或调整查询,只用改前台HTML即可,不用改后台代码 前台代码: <div style="pa ...
- Oracle 分区表中索引失效
当对分区表进行 一些操作时,会造成索引失效. 当有truncate/drop/exchange 操作分区 时全局索引 会失效. exchange 的临时表没有索引,或者有索引,没有用includin ...
- unity中数据的持久化存储
unity 提供了PlayerPrefs这个类用于存储游戏数据到电脑硬盘中. 这个类有10个函数可以使用 Class Functions类函数 SetInt Sets the value of the ...
- C#SaveFileDialog的使用
SaveFileDialog sfd = new SaveFileDialog(); //默认打开的路径 sfd.InitialDirectory = "C:\\Users\\Adminis ...
- JAVADOC 常见使用方法 帮助文档
我们知道Java中有三种注释语句: 1.//用于单行注释. 2./*...*/用于多行注释,从/*开始,到*/结束,不能嵌套. 3./**...*/则是为支持jdk工具javadoc.exe而特有的注 ...