Drying
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 12568   Accepted: 3243

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
题目大意:有n件衣服需要晾干,每件含水量ai.每件衣服每分钟自然干1单位的水.每分钟可以对其中任意一件使用吹风机,其可以减少k的水.求晾干所有衣服的最少时间.
思路分析:数据量很大,如果贪心做肯定会超时,而且这道题目是被归到二分专题中的,我自然而然的就选择用二分做了,二分算法,弱表才刚刚起步,在没有学之前,我觉
得二分就是在一个有序的数列中进行查找的时候会遇到,但随着学习的深入,渐渐感觉到了自己原来认识的浅薄,二分,不仅仅是查找!二分,相当于给了很多问题求解的另一种
思路,就是枚举,寻找最优解,通过二分枚举,可以最快的确定答案。这种题目的一般思路:
1.看数据范围和题目要求(最小最大),找到题眼,选用二分法。
2.确定二分边界,比如本题所要确定的边界就是需要的时间,很显然最小为1,最大为a[n-1](sort排序后).
3.开始二分逼近,寻找正确答案,此时的重点是check函数的构造,比如本题,给你一个时间t,你如何判断这些衣服在t时间内能否晾干,这就是check函数你要解决的问题。
比如本题,对于任何一个t,遍历数组a,如果a[i]<t,继续就行,若a[i]>t,则有下列方程,设烘干时间x1,自由蒸发时间x2,a[i]<=k*x1+x2,t=x1+x2.
可以求得x1>=(a[i]-t)/(k-1),向上取整即可,向上取整有技巧,你可以原式+1,然后分子减一,即(a[i]-k+k-2)/(k-1).什么时候不满足呢?自然是需要烘干的时间
>t时。至此check函数构造完毕。
tip:k==1时需单独处理。
弱重复使用字母导致报错。。。。心塞。。调试了半天
代码:
#include<iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
const int maxn=100000+100;
int a[maxn];
int n,k;
bool check(int t)
{
    int sum=0;
    if(k==1&&a[n-1]>t) return false;
    for(int i=0;i<n;i++)
    {
            if(a[i]<=t) continue;
            else
            {
            int b=(a[i]-t+k-2)/(k-1);
            sum+=b;
            if(sum>t) return false;
            }
    }
    return true;
}
int main()
{
    int ans;
   while(scanf("%d",&n)!=EOF)
   {
       for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
       sort(a,a+n);
       scanf("%d",&k);
       int l=1,r=a[n-1];
       while(l<=r)
       {
           int mid=(l+r)/2;
           if(check(mid)) ans=mid,r=mid-1;
           else l=mid+1;
       }
       printf("%d\n",ans);
   }
}

poj 3104 二分的更多相关文章

  1. Drying POJ - 3104 二分 最优

    题意:有N件湿的衣服,一台烘干机.每件衣服有一个湿度值.每秒会减一,如果用烘干机,每秒会减k.问最少多久可以晒完. 题解:二分.首先时间越长越容易晒完. 其次判定函数可以这样给出:对于答案 X,每一个 ...

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

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

  3. POJ - 2018 二分+单调子段和

    依然是学习分析方法的一道题 求一个长度为n的序列中的一个平均值最大且长度不小于L的子段,输出最大平均值 最值问题可二分,从而转变为判定性问题:是否存在长度大于等于L且平均值大于等于mid的字段和 每个 ...

  4. POJ 3104 Drying(二分答案)

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

  5. POJ 3104 Drying 二分

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

  6. POJ 3104 Drying(二分答案)

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

  7. poj 3104 Drying(二分查找)

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

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

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

  9. poj 3104 dring 二分

    Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7684   Accepted: 1967 Descriptio ...

随机推荐

  1. 【转】C++:MessageBox的常见用法

    一    函数原型及参数 function MessageBox(hWnd: HWND; Text, Caption: PChar; Type: Word): Integer; hWnd:对话框父窗口 ...

  2. 扩展编写jquery插件的方法

    比如要扩展验证功能(jquery.validate.js)中的 messages: { required: "This field is required.", remote: & ...

  3. windows下配置lamp环境(4)---安装MySQL数据库5.6

    图解安装MySQL数据库 1.获取软件就多不说了 2.双击开始安装 3.点击点击NEXT进行下一步 4.同意协议,点击NEXT进入下一步 5.选择指定安装位置安装方法,进入安装位值选择页面: 6.分别 ...

  4. Python新手学习基础之函数-概念与定义

    什么是函数? 函数是可以实现一些特定功能的方法或是程序,简单的理解下函数的概念,就是你编写了一些语句,为了方便使用,把这些语句组合在一起,给它起一个名字,即函数名.使用的时候只要调用这个名字,就可以实 ...

  5. Objective-C 静态变量 使用方法

    详解Objective-C中静态变量使用方法 Objective-C中静态变量使用方法是本文要介绍的内容,Objective-C 支持全局变量,主要有两种实现方式:第一种和C/C++中的一样,使用&q ...

  6. DLog 技巧

    #ifdef DEBUG#ifndef DLog# define DLog(fmt, ...) {NSLog((@"%s [Line %d] " fmt), __PRETTY_FU ...

  7. IOS NSURL基本操作-备

    NSURL其实就是我们在浏览器上看到的网站地址,这不就是一个字符串么,为什么还要在写一个NSURL呢,主要是因为网站地址的字符串都比较复杂,包括很多请求参数,这样在请求过程中需要解析出来每个部门,所以 ...

  8. caffe 中的一些参数介绍

    转自:http://blog.csdn.net/cyh_24/article/details/51537709 solver.prototxt net: "models/bvlc_alexn ...

  9. QT中读取文本数据(txt)

    下面的代码实现读取txt文档中的数据,并且是一行一行的读取. void MainWindow::on_pushButton_clicked() { QFile file("abcd.txt& ...

  10. Codeforces 13C Sequence

    http://codeforces.com/contest/13/problem/C 题目大意 给定一个含有N个数的序列,要求你对一些数减掉或者加上某个值,使得序列变为非递减的,问你加减的值的总和最少 ...