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 ...
随机推荐
- andorid EditView
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android=" ...
- Pandas选择数据
1.简单筛选 >>> dates = pd.date_range(', periods=6) >>> df = pd.DataFrame(np.arange(24) ...
- Docker原生网络技术简介
Docker原生网络技术简介 默认网络 在宿主机部署好Docker Engine后会默认创建三种网络:Bridge.Host和None,如下: docker network ls NETWORK ID ...
- Thrift.1
1. 依据thrift生成相对应语言的代码 [Ref]: http://wiki.apache.org/thrift/ThriftGeneration [Todo] 2. 如何使用生成的代码 [Ref ...
- 添加exe为windows service服务
[方法一] 一.介绍 srvany.exe是Microsoft Windows Resource Kits工具集的一个实用小工具,用于将EXE程序作为Windows服务运行.srvany是其注册程序的 ...
- RabbitVCS - Ubuntu VCS Graphical Client
Easy version control for Linux RabbitVCS is a set of graphical tools written to provide simple and s ...
- XAML中用一字符即可展示漂亮的图型
XAML中用一字符即可展示漂亮的图型 例如:Symbol Icon: People http://www.geekchamp.com/icon-explorer/action-icons/icon?c ...
- Viewer.js 是一款强大的 jQuery 图像浏览插件。
https://blog.csdn.net/qq_29132907/article/details/80136023 一.效果图 二.代码<!DOCTYPE html><html ...
- Python sys.argv[] 的用法
sys.argv变量是一个list, 执行 python abc.py a b c 时, sys.argv[0]为 abc.py sys.argv[1]为 a sys.argv[2]为 b sys.a ...
- 如何将本地代码通过git上传到码云
ps:同部署到GitHub上一样 http://www.cnblogs.com/pcx105/p/7777932.html