Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 13703   Accepted: 3527

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

Source

Northeastern Europe 2005, Northern Subregion

这是一道潮湿的题……

二分测试答案,看用这个时间能不能把所有衣服都烘干。

 /*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int mxn=;
int n;long long k;
long long a[mxn];
long long ans=;
long long calc(long long x){
int i,j;
long long cnt=;
for(i=;i<=n;i++){
if(a[i]>x){
long long xx = ceil((a[i]-x) * 1.0 / (k - ) );
cnt+=xx;
}
}
return cnt;
}
int main(){
scanf("%d",&n);
int i,j;
long long mxt=;
for(i=;i<=n;i++)
scanf("%lld",&a[i]),mxt=max(mxt,a[i]);
scanf("%lld",&k);
if(k==){
printf("%lld\n",mxt);
return ;
}
ans=;
long long l=,r=mxt;
while(l<=r){
long long mid=(l+r)/;
long long res=calc(mid);
if(res<=mid){
ans=mid;
r=mid-;
}
else l=mid+;
}
printf("%lld\n",ans);
return ;
}

POJ3104 Drying的更多相关文章

  1. POJ3104 Drying(二分查找)

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

  2. POj3104 Drying(二分)

    Drying Time Limit: 2000MS Memory Limit: 65536K Description It is very hard to wash and especially to ...

  3. poj3104 Drying(二分最大化最小值 好题)

    https://vjudge.net/problem/POJ-3104 一开始思路不对,一直在想怎么贪心,或者套优先队列.. 其实是用二分法.感觉二分法求最值很常用啊,稍微有点思路的二分就是先推出公式 ...

  4. POJ3104 Drying 2017-05-09 23:33 41人阅读 评论(0) 收藏

    Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15604   Accepted: 3976 Descripti ...

  5. POJ3104 Drying —— 二分

    题目链接:http://poj.org/problem?id=3104 Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissio ...

  6. POj-3104 Drying 二分+贪心

    题目大意:有n件湿的衣服,每件衣服都有相应的湿度,每分钟每件衣服的湿度减1(除了在烘干机里的衣服),现在有一个烘干机,烘干机一分钟可以让一件衣服的湿度降低k,问至少要花多少分钟才能使每件衣服的湿度为0 ...

  7. Drying poj3104(二分)

    Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7916   Accepted: 2006 Descriptio ...

  8. Drying [POJ3104] [二分答案]

    背景 每件衣服都有一定单位水分,在不适用烘干器的情况下,每件衣服每分钟自然流失1个单位水分,但如果使用了烘干机则每分钟流失K个单位水分,但是遗憾是只有1台烘干机,每台烘干机同时只能烘干1件衣服,请问要 ...

  9. 【POJ - 3104 】Drying(二分)

    Drying 直接上中文 Descriptions 每件衣服都有一定单位水分,在不使用烘干器的情况下,每件衣服每分钟自然流失1个单位水分,但如果使用了烘干机则每分钟流失K个单位水分,但是遗憾是只有1台 ...

随机推荐

  1. Binary Agents-freecodecamp算法题目

    Binary Agents 1.要求 传入二进制字符串,翻译成英语句子并返回. 二进制字符串是以空格分隔的. 2.思路 用.split(' ')将输入二进制字符串转化为各个二进制数字符串组成的数组 用 ...

  2. SAP系统管理中常见问题解答(转载)

    1.如何查看SAP系统的位数? system——status看 Platform ID Platform 32-bit 64-bit --------------------------------- ...

  3. Mysql--数据操作语言(DML)

    定义:数据操作语言主要实现对数据库表中的数据进行操作,主要包括插入(insert).更新(update).删除(delete).查询(select),本节主要介绍增删改. 数据准备: 一.数据的插入( ...

  4. 【CodeBase】PHP检查未知媒体文件的格式

    用法: <?php $filefullpath="F:/test/2awd45wr1e5fef5e5"; echo Format::check($filefullpath,[ ...

  5. tcl之string操作

  6. 数据结构-队列(Queue)

    #include <stdio.h> #include <stdlib.h> #define LIST_INIT_SIZE 10 #define LISTINCREMENT 1 ...

  7. python3 爬取汽车之家所有车型数据操作步骤(更新版)

    题记: 互联网上关于使用python3去爬取汽车之家的汽车数据(主要是汽车基本参数,配置参数,颜色参数,内饰参数)的教程已经非常多了,但大体的方案分两种: 1.解析出汽车之家某个车型的网页,然后正则表 ...

  8. django之配置静态文件

    # 别名 STATIC_URL = '/static/' # 配置静态文件,名字必须是STATICFILES_DIRS STATICFILES_DIRS = [ os.path.join(BASE_D ...

  9. CCPC_1005

    可怕.....的提.....显而易见的规律活活没照出来...不过说起来却是不能严格证明....于是...脑筋急转弯活活猜不出来..... 1*1->1*2->2*2->2*3-> ...

  10. Spark性能优化:开发调优篇

    1.前言 在大数据计算领域,Spark已经成为了越来越流行.越来越受欢迎的计算平台之一.Spark的功能涵盖了大数据领域的离线批处理.SQL类处理.流式/实时计算.机器学习.图计算等各种不同类型的计算 ...