POJ3104(二分搜索)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 13057 | Accepted: 3358 |
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 kwater, 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 #1
3
2 3 9
5 sample input #2
3
2 3 6
5
Sample Output
sample output #1
3 sample output #2
2
思路:二分minute。设每个clothes所需y次烘干,那么就有minute-y次自然干。minute-y+y*k>=x[i]。解y>=(x[i]-minute)/(k-1)。注意k等于1的情况。
#include <cstdio>
#include <algorithm>
#include <math.h>
using namespace std;
const int MAXN=;
int n,k;
int x[MAXN];
bool test(int t)
{
int s=;
for(int i=;i<n;i++)
{
if(x[i]<=t) continue;
s+=(int)ceil((x[i]-t)*1.0/(k-));
if(s>t) return false;
}
return true;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
int mx=;
for(int i=;i<n;i++)
{
scanf("%d",&x[i]);
mx=max(mx,x[i]);
}
scanf("%d",&k);
if(k==)
{
printf("%d\n",mx);
}
else
{
int l=;
int r=0x3f3f3f3f;
while(r-l>)
{
int mid=(l+r)>>;
if(test(mid)) r=mid;
else l=mid;
}
printf("%d\n",r);
}
}
return ;
}
POJ3104(二分搜索)的更多相关文章
- [LeetCode] Largest BST Subtree 最大的二分搜索子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- hdu 2199 Can you solve this equation?(二分搜索)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdu 2199:Can you solve this equation?(二分搜索)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- 二分搜索 UVALive 6076 Yukari's Birthday (12长春K)
题目传送门 题意:问使得sum (k^i) = n || n -1 (1 <= i <= r) 的min (r*k)组合的r和k 分析:r的最大不会超过40,枚举r,二分搜索k.注意会爆 ...
- hdu 1075 二分搜索
还是写一下,二分搜索好了 这道题开数组比较坑... 二分,需要注意边界问题,例如:左闭右闭,左闭右开,否则查找不到or死循环 先上AC代码 #include<iostream> #incl ...
- K Best(最大化平均数)_二分搜索
Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...
- HDU 2852 KiKi's K-Number(树状数组+二分搜索)
题意:给出三种操作 0 e:将e放入容器中 1 e:将e从容器中删除,若不存在,则输出No Elment! 2 a k:搜索容器中比a大的第k个数,若不存在,则输出Not Find! 思路:树状数组+ ...
- nyoj914Yougth的最大化(二分搜索 + 贪心)
Yougth的最大化 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Yougth现在有n个物品的重量和价值分别是Wi和Vi,你能帮他从中选出k个物品使得单位重量的价值最大吗 ...
- POJ3104 Drying(二分查找)
POJ3104 Drying 这个题由于题目数据比较大(1 ≤ ai ≤ 109),采用贪心的话肯定会超时,自然就会想到用二分. 设C(x)为true时表示所用时间为X时,可以把所有的衣服都烘干或者自 ...
随机推荐
- Could not autowire field: private javax.servlet.http.HttpServletRequest
在写单元测试类的时候,报错,废了很大劲才给调试好,给大家分享下. 完整错误如下: Caused by: org.springframework.beans.factory.BeanCreationEx ...
- Druid数据库连接池的一般使用
据说:阿里的Druid这款产品,是目前最好用的数据库池产品,下面就来看下怎么在我们项目中去使用它吧. 项目背景:使用的是SpringMvc+Spring+mybatis 在ssm框架里面使用数据连接池 ...
- Ubuntu 16.04 安装 RabbitMQ
Ubuntu 16.04 安装 RabbitMQ(注意,服务器安全组需要添加15672 和5672端口) #1 更新 $ sudo apt-get update$ sudo apt-get upgra ...
- Mac键盘图标与对应快捷按键标志汇总 分类
Mac键盘图标与对应快捷按键 ⌘——Command () win键 ⌃ ——Control ctrl键 ⌥——Option (alt) ⇧——Shift ⇪——Caps Lock fn——功能键就是 ...
- js代理模式
代理模式的关键是,当客户不方便直接访问一个对象或者不满足需要的时候,提供一个替身对象来控制对这个对象的访问,客户实际上访问的是替身对象.替身对象对请求做出一些处理之后,再把请求转交给本体对象. 代理模 ...
- Tair分布式缓存
淘宝缓存架构 redis很好用,提供缓存服务.相比memcached多了新数据结构和主从模式增加可用性.不过redis有一点不能满足一些互联网公司开发者需求. re ...
- JavaScript 获取输入时的光标位置及场景问题
前言 在输入编辑的业务场景中,可能会需要在光标当前的位置或附近显示提示选项.比如社交评论中的@user功能,要确保提示的用户列表总是出现在@字符右下方,又或者是在自定义编辑器中 autocomplet ...
- 1、C#中Hashtable、Dictionary详解以及写入和读取对比
在本文中将从基础角度讲解HashTable.Dictionary的构造和通过程序进行插入读取对比. 一:HashTable 1.HashTable是一种散列表,他内部维护很多对Key-Value键值对 ...
- IIS7 配置PHP服务器
安装PHP Manager: 1)访问 http://phpmanager.codeplex.com/releases/view/69115 下载PHP Manager.其中,x86 为32位 Win ...
- Python — 多线程与多进程
1.多线程 线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位,一个进程可以包含多个线程.一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线 ...