• poj 3104 二分


    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 ",ans);
       }
    }
  • 相关阅读:
    LeetCode第1题(two sum) 暴力法性能优化
    java.sql.SQLException: Column 'id' not found.
    数据分页显示 之 确定总页码数(优化)
    IntelliJ IDEA 提示"Form input without an associated label or title attribute"
    在JSP中图片上传到服务器后无法读取(已解决)
    The maximum-subarray problem
    Chapter 2:Getting Started
    Merge Sort in Java, C,C++ and Python
    Chapter 9 :Further Applications of Integration
    gSoap 中文传输
  • 原文地址:https://www.cnblogs.com/xuejianye/p/5506467.html
Copyright © 2020-2023  润新知