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

    1. 二分枚举时间。
    2. 小于等于枚举时间mid的不用管。
    3. 根据公式a[i] - k*t <= mid-t ==> t >= (a[i]-mid)/(k-1),要对后面取整。
    4. 如果最后时间加起来超过了mid。那mid太小。不行。
    5. 最后保存的是返回true的那个r。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cmath>
     5 using namespace std;
     6 long long a[1000005];
     7 long long n,k;
     8 /*
     9 bool check(int mid)
    10 {
    11     int src = mid;
    12     int cur;
    13     for(int i=n;i>=1;i--)
    14     {
    15         cur = a[i];
    16         if(cur<=src)
    17             continue;
    18         else
    19         {
    20             while(cur>src)
    21             {
    22                 cur -=k-1;
    23                 mid--;
    24             }
    25         }
    26     }
    27     if(mid>=0)
    28         return true;
    29     else
    30         return false;
    31 }*/
    32 
    33 bool check(long long  mid)
    34 {
    35    unsigned long long sum(0);
    36    for(int i = 1;i <=n;++i)
    37    {
    38       long long more = a[i] - mid;
    39       if(more > 0)
    40       {
    41          sum += ((more + k - 1) / k);//或者在这里判断一下是否可以整除,然后分开加。
    42       }
    43    }
    44    return sum <= mid;
    45 }
    46 int main()
    47 {
    48     while(~scanf("%lld",&n))
    49     {
    50         long long r = 0;
    51         for(int i=1;i<=n;i++)
    52         {
    53             scanf("%lld",&a[i]);
    54             r = max(r,a[i]);
    55         }
    56         scanf("%d",&k);
    57         if(k==1)
    58         {
    59             printf("%lld
    ",r);
    60             continue;
    61         }
    62         k--;
    63         long long l = -1;
    64         long long mid;
    65         while(r-l>1)
    66         {
    67             mid = (r+l)>>1;
    68             if(check(mid))
    69                 r = mid;
    70             else
    71                 l = mid;
    72         }
    73         printf("%lld
    ",r);
    74     }
    75     return 0;
    76 }
    注:转载请注明出处
  • 相关阅读:
    public static void Invoke (Action action)
    C#编写WIN32系统托盘程序
    C#的互操作性:缓冲区、结构、指针
    SQLServer异步调用,批量复制
    Python体验(10)-图形界面之计算器
    Python体验(09)-图形界面之Pannel和Sizer
    Python体验(08)-图形界面之工具栏和状态栏
    Python体验(07)-图形界面之菜单
    C#利用WIN32实现按键注册
    Javascript猜数字游戏
  • 原文地址:https://www.cnblogs.com/1625--H/p/9368626.html
Copyright © 2011-2022 走看看