Drying
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(二分搜索)的更多相关文章

  1. [LeetCode] Largest BST Subtree 最大的二分搜索子树

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  2. hdu 2199 Can you solve this equation?(二分搜索)

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  3. hdu 2199:Can you solve this equation?(二分搜索)

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  4. 二分搜索 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.注意会爆 ...

  5. hdu 1075 二分搜索

    还是写一下,二分搜索好了 这道题开数组比较坑... 二分,需要注意边界问题,例如:左闭右闭,左闭右开,否则查找不到or死循环 先上AC代码 #include<iostream> #incl ...

  6. K Best(最大化平均数)_二分搜索

    Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...

  7. HDU 2852 KiKi's K-Number(树状数组+二分搜索)

    题意:给出三种操作 0 e:将e放入容器中 1 e:将e从容器中删除,若不存在,则输出No Elment! 2 a k:搜索容器中比a大的第k个数,若不存在,则输出Not Find! 思路:树状数组+ ...

  8. nyoj914Yougth的最大化(二分搜索 + 贪心)

    Yougth的最大化 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Yougth现在有n个物品的重量和价值分别是Wi和Vi,你能帮他从中选出k个物品使得单位重量的价值最大吗 ...

  9. POJ3104 Drying(二分查找)

    POJ3104 Drying 这个题由于题目数据比较大(1 ≤ ai ≤ 109),采用贪心的话肯定会超时,自然就会想到用二分. 设C(x)为true时表示所用时间为X时,可以把所有的衣服都烘干或者自 ...

随机推荐

  1. WebLogic 12c 多节点Cluster静默安装

    WebLogic集群架构 Weblogic角色 AdminServer: 172.16.65.130 NodeServer: 172.16.65.131.172.16.65.132 版本 weblog ...

  2. centos6 多段Ip添加脚本

    #!/bin/bash export device=`ifconfig|grep eth0|head -n 1|awk '{print ($1)}'`export ipcfg_pre="/e ...

  3. 基于“基于dockerhub的jetty镜像的ossfs镜像”部署war包,遇到的文件夹读写权限被限制的问题解决方案

    前提: “基于dockerhub的jetty镜像的ossfs镜像” 已经搭建好了. 部署准备: 1.本地打包:war包-->idea工具 mvn 打包. 2.本地sh脚本:compile_vps ...

  4. Java -- 数据库 多表操作,1对多,多对多,1对1。 基于dbutils框架

    1. 1对多,部门--员工 为例, 多的一方建外键. domain,建立bean对象 public class Department { private String id; private Stri ...

  5. 有若干个箱子,假设每个箱子的最大承重为 MaxW 。将货物分配装箱

    今天在博客园中看到一个博问,就写了下实现代码. 问题: 有若干个箱子,假设每个箱子的最大承重为 MaxW .有一批物品,它们的重量分别为w1.w2...Wn,假设每个物品的重量都不超过箱子承重.写个算 ...

  6. 判断一个浏览器是否支持opacity

    支持opacity的浏览器,总会将opacity值规范成小于1.0且以0开头的值.例如,如果将opacity指定为:.5,原始支持opacity的浏览器就会将该值规范为0.5,而不支持opacity的 ...

  7. 快速的熟悉一个angular的项目从run看起

    config之类的都会注入到controller或者run里边

  8. Dom节点操作常用方法

    1.访问/获取节点 document.getElementById(id); //返回对拥有指定id的第一个对象进行访问 document.getElementsByName(name); //返回带 ...

  9. 编译内核时覆盖KBUILD_BUILD_USER和KBUILD_BUILD_HOST

    默认情况下make kernel.img编译出来的内核在/proc/version中显示的内容是: Linux version 3.0.36+ (xxx@yyyy) (gcc version 4.6. ...

  10. windows下安装virtualenvwrapper之后workon不是内部或外部指令

    virtualenvwrapper是虚拟环境的操作,在windows下需要使用以下命令安装: pip install virtualenvwrapper-win 安装win下的环境 相关操作:work ...