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)的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  3. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  4. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  10. [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 ...

随机推荐

  1. 页面练习my blog day51

    html端: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  2. VSS源代码管理器运行代码分析工具的命令

    当你发现代码库总是报需要联系管理员运行代码分析工具时,你可以使用命令分析代码库代码解决: To fix the database problems, you can restart the analy ...

  3. PL/SQL的快捷键设置

    PL/SQL用来连接Oracle数据库的一种工具,它可以设置快捷方式,以便于我们快速的操作. PL/SQL设置快捷键    tools->Preferences(首选项)->User In ...

  4. php伪静态与重定向

    什么是伪静态 伪静态是相对于真静态来讲的,伪静态只是改变了URL格式,实际还是动态页面,有真静态一样的SEO,真静态访问一个静态页面,服务器(apache,nginx)直接读取磁盘静态文件,伪静态是动 ...

  5. 1N - 计算球体积

    根据输入的半径值,计算球的体积. Input 输入数据有多组,每组占一行,每行包括一个实数,表示球的半径. Output 输出对应的球的体积,对于每组输入数据,输出一行,计算结果保留三位小数. Sam ...

  6. Oracle数据库mybatis 插入空值时报错(with JdbcType OTHER)

    参考文档: 1.https://blog.csdn.net/fishernemo/article/details/27649233 2.http://helgaxu.iteye.com/blog/21 ...

  7. tp5安装验证码

  8. Spring 中的类加载机制 - ClassLoader

    Spring 中的类加载机制 - ClassLoader Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) ClassLoa ...

  9. Python之路(第十二篇)程序解耦、模块介绍\导入\安装、包

    一.程序解耦 解耦总的一句话来说,减少依赖,抽象业务和逻辑,让各个功能实现独立. 直观理解“解耦”,就是我可以替换某个模块,对原来系统的功能不造成影响.是两个东西原来互相影响,现在让他们独立发展:核心 ...

  10. Two Sum LT1

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...