Drying

Time Limit: 2000MS Memory Limit: 65536K

Total Submissions: 20586 Accepted: 5186

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


解题心得:

  1. 题意就是有n件衣服,每件衣服有一个湿润度,有一个烘干机,每次可以烘干一件衣服,每分钟烘干k的水分,每分钟可以选择放入/拿出一件衣服,如果衣服没有放入烘干机那么衣服每分钟会自然蒸发1水分。问怎么才能将所有衣服烘干并且所花时间要最少,最少时间是多少。
  2. 其实难点是如果不放入烘干机中衣服每分钟会自然蒸发1水分,那么有没有办法将自然蒸发这个条件去掉呢,其实还是可以的,我们可以假设用了m分钟,那么自然烘干的水分就是每件衣服m的水分,剩下的水分就是使用烘干机烘干的。但是每次枚举m分钟肯定会超时,那么就可以用到二分了,如果枚举出来的时间可以将衣服全烘干,那么就可以缩小时间,如果不能就扩大时间,二分得到最后的答案。

#include <algorithm>
#include <stdio.h>
#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn = 1e5+100;
int n,k,water[maxn]; void init() {
for(int i=0;i<n;i++)
scanf("%d",&water[i]);
scanf("%d",&k);
} bool checke(ll time) {
ll min_time = 0;
for(int i=0;i<n;i++) {
if(water[i] <= time)
continue;
else {
int temp = water[i] - time;
min_time += temp/(k-1);
if(temp % (k-1))
min_time++;
}
}
if(min_time <= time)
return true;
return false;
} ll binary_search() {
ll l,r;
l = 0, r = 1e9+100;
while(r - l > 1) {
ll mid = (l+r) >> 1;
if(checke(mid))
r = mid;
else
l = mid;
}
return r;
} int main() {
while(scanf("%d",&n) != EOF) {
init();
if(k == 1) {
int Max = -1;
for(int i=0;i<n;i++)
Max = max(Max,water[i]);
printf("%d\n",Max);
continue;
}
ll ans = binary_search();
printf("%lld\n",ans);
}
return 0;
}

POJ:3104-Drying(神奇的二分)的更多相关文章

  1. POJ 3104 Drying (经典)【二分答案】

    <题目链接> 题目大意: 有一些衣服,每件衣服有一定水量,有一个烘干机,每次可以烘一件衣服,每分钟可以烘掉k滴水.每件衣服没分钟可以自动蒸发掉一滴水,用烘干机烘衣服时不蒸发.问最少需要多少 ...

  2. POJ 3104 Drying(二分答案)

    题目链接:http://poj.org/problem?id=3104                                                                  ...

  3. POJ 3104 Drying 二分

    http://poj.org/problem?id=3104 题目大意: 有n件衣服,每件有ai的水,自然风干每分钟少1,而烘干每分钟少k.求所有弄干的最短时间. 思路: 注意烘干时候没有自然风干. ...

  4. POJ 3104 Drying(二分答案)

    [题目链接] http://poj.org/problem?id=3104 [题目大意] 给出n件需要干燥的衣服,烘干机能够每秒干燥k水分, 不在烘干的衣服本身每秒能干燥1水分 求出最少需要干燥的时间 ...

  5. poj 3104 Drying(二分查找)

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

  6. POJ 3104 Drying [二分 有坑点 好题]

    传送门 表示又是神题一道 Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9327   Accepted: 23 ...

  7. POJ 3104 Drying(二分

    Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22163   Accepted: 5611 Descripti ...

  8. POJ 3104 Drying (二分+精度)

    题目链接:click here~~ [题目大意]: 题意:有一些衣服,每件衣服有一定水量,有一个烘干机,每次能够烘一件衣服,每分钟能够烘掉k单位水. 每件衣服没分钟能够自己主动蒸发掉一单位水, 用烘干 ...

  9. poj 3104 Drying(二分搜索之最大化最小值)

    Description It is very hard to wash and especially to dry clothes in winter. But Jane is a very smar ...

  10. POJ 3104 Drying

    最让HSQ学长头疼的就是洗衣服了.洗完之后,每件衣服都有一定单位水分,在不使用烘干器的情况下,每件衣服每分钟自然流失1个单位水分,但如果使用了烘干机则每分钟流失K个单位水分.令人遗憾是HSQ所在的宿舍 ...

随机推荐

  1. Brackets - 前端神器

    做了几年的 .Net 项目开发,后来公司转 Java 语言开发,Java 做了还没一年,公司准备前后端分离开发,而我被分到前端! Brackets是一款基于web(html+css+js)开发的web ...

  2. CODESOFT条码设计软件如何隐藏数据源方法

    作为强大的条码标签设计软件,用户在用CODESOFT设计条码标签时,有时需要根据实际情况,将条码数据源隐藏,也就是使设计与打印出来的条形码下不带有数据.那么这要怎么在CODESOFT中实现呢?下面,小 ...

  3. Linux MySQL单实例源码编译安装5.5.32

    cmake软件 tar -zxvf cmake-2.8.8.tar.gz cd cmake-2.8.8 ./bootstrap make make install cd ../   依赖包 yum i ...

  4. SPOJ - ORDERS--- Ordering the Soldiers---根据逆序对求原数组

    题目链接: https://vjudge.net/problem/SPOJ-ORDERS 题目大意: 根据每个数字的逆序对求出原数组 解题思路: 举个例子: n = 5 a[ n ] = { 0, 1 ...

  5. HDU(1754),线段树,单点替换,区间最值

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 线段树模板题,update功能是单点替换,query是访问区间最大值. #include < ...

  6. UIView设置阴影无效的原因之一

    本想在底部的按钮设置个阴影, 代码如下: self.layer.shadowColor = [UIColor blackColor].CGColor; self.layer.shadowOffset ...

  7. 奇异值分解(SVD)原理及应用

    一.奇异值与特征值基础知识: 特征值分解和奇异值分解在机器学习领域都是属于满地可见的方法.两者有着很紧密的关系,我在接下来会谈到,特征值分解和奇异值分解的目的都是一样,就是提取出一个矩阵最重要的特征. ...

  8. Mac 修改用户环境变量

    Mac 修改用户环境变量 sudo vim ~/.bash_profile

  9. Ajax的学习笔记(一)

    AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),ajax并不是一门单独的语言,而是一种技术,是指一种创建交互式网页应用的网页开发技术. ...

  10. 旧文备份:对象字典0x1005和0x1006的理解

    SYNC不一定由主站产生,因此,产生SYNC的节点,0x1005对象的值一般是0x40000080,第30位为1表示本节点产生 SYNC,而本节点的0x1006对象就是产生同步周期值了;而接收SYNC ...