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时,可以把所有的衣服都烘干或者自 ...
随机推荐
- Kubernetes Horizontal Pod Autoscaler
非常牛逼的技术,目前最新的版本支持众多的Feature HPA功能需要Heapster收集的CPU.内存等数据作为支撑 配置示例: apiVersion: autoscaling/v2beta1 ki ...
- Shell脚本实现SSH免密登录及批量配置管理
本节索引 场景分析 ssh免密登录 pssh工具批量管理 SHELL自动化脚本 本篇总结 场景分析 作为一个运维工程师,不是每个人工作的环境都想阿里.腾讯那样,动不动就上亿的PV量,上万台服务器.我们 ...
- 基于docker环境,搭建 jetty环境, 部署java项目
前提: 1.Ubuntu 系统. 2.docker环境已经安装好. 实现步骤: 1.上docker hub 下载jetty docker 镜像. 执行命令:$ sudo docker pull jet ...
- EF Code-First 学习之旅 DataAnnotations
数据注解:配置选项的子集:Fluent API包含所有选项 System.ComponentModel.DataAnnotations Attributes: Attribute Descriptio ...
- java 开发面试题小整理(一)
本篇文档将持续更新,有基础滴,也有深层次的,谢谢! 1.看下面的程序是否有问题,如果有问题,请指出并说明理由. * byte b1 = 3; * byte b2 = 4; * byte b3 = b1 ...
- Vue v-on v-model 组合使用
v-on vue可以使用v-on指令来监听事件,方便与用户进行交互.我们不需要修改DOM中的数据,所有的操作都由Vue来实现,你编写的代码只需要关注底层逻辑.这也是Vue强大的地方之一 <!DO ...
- Spring Boot 注释
1.@RestController@RestController ≍ @Controller + @ResponseBody在Controller文件 public class xxxx 前面加用于返 ...
- 运行php的时候出现计算机中丢失 MSVCR110.dll怎么解决
运行php的时候出现计算机中丢失 MSVCR110.dll怎么解决 一.总结 一句话总结:因为现在php所有的 5.5 环境都是基于 vc11 的编译脚本下生成的,所以在 windows 下你得安装相 ...
- 打包的时候遇上找不到dll文件错误
1.保证dll文件和EXE文件处于同级目录下 我是在EXE同级文件目录下建立了一个Plugins文件并把dll文件夹放在这里面 2.但是因为建立的目录是x86_64,所以如果打包成windows平台选 ...
- 使用JMeter建立接口测试
[需求]某组机器是Android和iOS输入法接口服务器,有很多重要的接口,例如:升级,网络开关,热词等.现在有3台机器过保要下线,新申请了3台机器,需要验证一下这3台机器接口的正确性. [测试步骤] ...