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. body-parser小解

    body-parser用来解析http请求体,对不同的content-type有不同的处理方式, 首先介绍一下常见的四种Content-Type: 1.application/x-www-form-u ...

  2. Golang 高性能UDP Server实现

    通过Goroutine实现UDP消息并发处理 package main import ( "net" "fmt" "os" ) // 限制g ...

  3. Ubuntu 配置NTP Server

    Ubuntu安装NTP Server很简单,分位3步走: 第一步:安装NTP root@cephadmin:~/ceph-cluster# apt-get install ntp Reading pa ...

  4. ceph安装各种报错

    [ceph_deploy][ERROR ] RuntimeError: Failed to execute command: ceph-disk-activate –mark-init sysvini ...

  5. 在一个N个整数数组里面,有多个奇数和偶数,设计一个排序算法,令所有的奇数都在左边。

    //在一个N个整数数组里面,有多个奇数和偶数,设计一个排序算法,令所有的奇数都在左边. // 例如: 当输入a = {8,4,1,6,7,4,9,6,4}, // a = {1,7,9,8,4,6,4 ...

  6. CCNA 课程 二

    传输层:两个重要的协议 TCP 和 UDP TCP: 面向连接的协议:在传输用户数据前,先要建立连接 (TCP的三次握手) 错误检查 数据包序列化 可靠性传输:发送的数据需要接受者提供确认,通过报头中 ...

  7. java hasmap对象的深复制实现:字节码复制和对象序列化成字符串复制比较。

    /** * Created by Administrator on 2016/11/23. */ public class test { public static void main(String[ ...

  8. linux基础(3)-java安装

    安装jdk1.8 [root@spark1 usr]# mkdir java #创建java目录 通过WinSCP将jdk-8u77-linux-x64.tar.gz传到/usr/java目录下 [r ...

  9. 初探MyBatis之HelloWorld(二)

    二.不使用 XML 构建 SqlSessionFactory 不使用xml构建SqlSessionFactory的话,就要用java代码来连接数据库.我这里直接new DataSorce()接口实现g ...

  10. iOS审核拒绝苹果官方原因详解

    1.1不当内容应用程序不应该包括攻击性,敏感,令人不悦,侮辱或者品味低下的内容.例如: 1.1.1 诽谤或者人格侮辱的内容,包括引用或者评论宗教.种族.性取向.性别或者其他目标群体的内容,特别是该应用 ...