zoukankan      html  css  js  c++  java
  • Drying POJ

      

      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.不要随便用upper_bound,如果你没理解清楚的话~~~(wa了十多次)
    2.二分答案再验证,在验证的时候注意方法~~~
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<string>
     5 #define MAX 100005
     6 using namespace std;
     7 typedef long long ll;
     8 
     9 ll n,m;
    10 ll a[MAX];
    11 
    12 bool check(int k){
    13     ll cnt=0;
    14     //ll pos=upper_bound(a+1,a+n+1,k)-a-1;
    15     //if(pos==0) pos++;
    16     for(int i=1;i<=n;i++){
    17         if(a[i]>k){
    18            cnt+=(a[i]-k)/(m-1);
    19            if((a[i]-k)%(m-1)!=0) cnt++;
    20            if(cnt>k) return false;
    21         }
    22     }
    23     return true;
    24 }
    25 int main()
    26 {  
    27    while(~scanf("%lld",&n)){
    28         for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
    29         sort(a+1,a+n+1);
    30         scanf("%lld",&m);
    31         if(m==1) printf("%lld
    ",a[n]);
    32         else{
    33             ll l=1,r=a[n];
    34             while(r-l>1){
    35                    ll mid=(l+r)/2;
    36                    if(check(mid)) r=mid;
    37                    else l=mid;
    38             } 
    39             if(check(l)) printf("%lld
    ",l);
    40             else printf("%lld
    ",r);
    41         }
    42     }
    43     return 0;
    44 }
  • 相关阅读:
    SQL语句(四)可视化创建和修改表
    (四)系统调用示例
    (三)系统调用
    (二) 中断、异常和系统调用比较
    (一)系统启动流程
    Bellman-Ford算法:POJ No.3169 Layout 差分约束
    kruskal算法:POJ No.3723 Conscription_最小生成树应用_最大权森林
    POJ No.3255 Roadblocks 求次短路径
    【Java】单例模式双重检查锁(double-checked locking)
    【Java多线程】volatile关键字解析(五)
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/6769666.html
Copyright © 2011-2022 走看看