POJ3104--Drying(Binary Search)
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 #1
3
2 3 9
5 sample input #2
3
2 3 6
5
Sample Output
sample output #1
3 sample output #2
2 这道题处理方法很不错。每分钟不放进烘干机会消耗1单位的水,而放进烘干机会消耗k。所以可以理解为,无论在哪都每分钟消耗1,而在烘干机里每分钟k-1。这样的话,问题就简化很多。
#include<iostream>
#include<numeric>
#include<algorithm>
using namespace std;
int clothes[];
int n,k; bool C(int d){
unsigned long long minutes=;
for(int i=;i<n;i++){
int remain=clothes[i]-d;
if(remain>){
minutes+=(remain+k-)/k;//ceil
if(minutes>d)
return false;
}
}
return true;
} int main(){
cin>>n;
for(int i=;i<n;i++)
cin>>clothes[i];
cin>>k;
k--;
if(k==){
cout<<*max_element(clothes,clothes+n)<<endl;
return ;
}
int lb=*min_element(clothes,clothes+n)/k;
int ub=*max_element(clothes,clothes+n);
while(ub-lb>){
int mid=(ub+lb)/;
if(C(mid))
ub=mid;
else
lb=mid;
}
cout<<ub<<endl;
return ;
}
POJ3104--Drying(Binary Search)的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
随机推荐
- 基于.net技术的 Rss 订阅开发
RSS(Really Simple Syndication,真正简单的连锁)是一种 Web 内容连锁格式.RSS 成为通过 Web 连锁新闻内容的标准格式.刚好我现在vs的环境也是.net,因为在.N ...
- 简单DP入门四连发
复习一下一直不太懂的dp. dp博大精深,路还长着呢 第一题;http://acm.hdu.edu.cn/showproblem.php?pid=2084 从下往上就是水题 #include<c ...
- dpdk优化相关 转
注:本文是参照了一些其他文章,原文地址点击这里. 首先根据这篇文章进行了性能瓶颈的分析 策略与方法 首先根据木桶原理,首先要找到最弱的地方,怎么找往上看↑. 想能优化需要考虑如下: 优化BIOS设置 ...
- Lazarus IOCP 移植
delphi下面有一个高性能IOCP库,是俄国人写的,在下将其移植到了lazarus下面, lazarus 版本 1.0.12 ,需要的下载 和indy相比较,indy开发的一个web服务器,cpu占 ...
- Javascript的一个怪现象
javascript有一个怪现象,就是减法也会导致小数位数问题,是一个麻烦的问题,比如. <html><script> var a=10,b=20.1; alert( a - ...
- java 环境搭建
一.安装jdk 下载jdk http://www.oracle.com/technetwork/java/javase/downloads 将下载的jdk文件放到 /opt 下解压 $sudo cp ...
- swift MD5 加密方法
引入OC类库 md5.h: #import <UIKit/UIKit.h> @interface Md5Controller : UIViewController @end md5.m: ...
- 《C#从现象到本质》读书笔记(二)第2章 C#类型基础(上)
<C#从现象到本质>读书笔记第二篇 第2章 C#类型基础(上) 类型指的是集合{类,结构,接口,枚举,委托}中的任意一个成员.任何拥有某类型的值(value)称为某类型的一个实例(inst ...
- JS Async Callback
AsyncCallback 意义: 异步操作完成时调用的方法 语法1: 构造异步回调对象 AsyncCallback 异步回调对象名asyncCallback = new AsyncCallback( ...
- 【算法】Escape
The students of the HEU are maneuvering for their military training. The red army and the blue army ...