zoukankan      html  css  js  c++  java
  • poj 3104 Drying(二分搜索之最大化最小值)

    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
     

    晾衣服:n件衣服各含a_i水分,自然干一分钟一单位,放烘干机一分钟k单位,一次只能晒一件。求最短时间。

    取C(mid) := 能在mid分钟内处理完,然后二分即可。

    这里有两个很好玩的陷阱

    ①每分钟烘干k单位的水,于是我就想当然地除k向上取整了((a_i – mid) / k)。其实应该除以k-1,列个详细的算式:

    设需要用x分钟的机器,那么自然风干需要mid – x分钟,x和mid需要满足:

    k*x + (mid – x) >= a_i,即 x >= (a_i – mid) / (k – 1)。

    ②当k=1的时候,很显然会发生除零错误,需要特殊处理。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<cmath>
     6 #include<stdlib.h>
     7 using namespace std;
     8 #define N 100006
     9 #define ll long long
    10 ll n;
    11 ll k;
    12 ll a[N];
    13 bool solve(ll mid){
    14     ll minute=0;
    15     for(ll i=0;i<n;i++){
    16         if(a[i]>mid){
    17             minute+=(ceil((a[i]-mid)*1.0/(k-1)));
    18         }
    19     }
    20     if(minute>mid) return false;
    21     return true;
    22 
    23 }
    24 int main()
    25 {
    26     int ac=0;
    27     while(scanf("%I64d",&n)==1){
    28 
    29         ll low=1;
    30         ll high=0;
    31         for(ll i=0;i<n;i++){
    32             scanf("%I64d",&a[i]);
    33             high=max(high,a[i]);
    34         }
    35         scanf("%I64d",&k);
    36         if(k==1){
    37             printf("%I64d
    ",high);
    38             continue;
    39         }
    40         ll ans;
    41         while(low<high){
    42 
    43             ll mid=(low+high)>>1;
    44             if(solve(mid)){
    45                 high=mid;
    46             }
    47             else{
    48                 low=mid+1;
    49                 
    50             }
    51         }
    52 
    53         printf("%I64d
    ",low);
    54     }
    55     return 0;
    56 }
    View Code
  • 相关阅读:
    HDU 1004 Let the Balloon Rise【STL<map>】
    UVA 1030
    UVA 10881
    POJ 3154 Graveyard【多解,数论,贪心】
    浅谈Notepad++选中行操作+快捷键+使用技巧【超详解】
    COGS 68. [NOIP2005] 采药【01背包复习】
    [phomeflashpic]怎样调用帝国CMS图片幻灯效果
    微信认证新增公对公账户银行卡转账支付审核费用 缩减认证审核时长
    微信公众平台回复过了怎么不能再次回复?亲们要注意查看"公众平台回复用户消息时限变更通知"的公告啊
    新版微信终于支持消息撤回了 微信零钱也能转账了[微信5.3.1.16更新]
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4780612.html
Copyright © 2011-2022 走看看