POJ Drying

Drying

题目链接:Click Here~

题目分析:

给出N件带水的衣服,你有两种选择可以把某件衣服给弄干。一是用烘干机可以每分钟烤干衣服的K滴水。二是每分钟衣服会自然烘干一滴水。而用烘干机的时候就不再自然烘干了。而每件衣服所带的水滴是不一样多的。现在问你最少要多少时间可以把衣服全烘干。

思路分析:

先二分枚举时间。但是判断的条件有点难想到,一开始用暴力判断结果超时了。后来看了博客后才知道只要转换一下公式就可以了判断了。详细解释过程请看代码。

#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

const int MAXN = 100000 + 10;
const int INF = ~0U >> 2;

int a[MAXN],sum[MAXN];
int N,K;

/*

  假设烘干时间是x,则自然风干时间为mid - x
  K*x + (mid - x) >= a[i]
  x >= (a[i] - mid)/(K - 1) (K != 1)

*/

bool C(int mid){
   unsigned long long sum(0);

   for(int i = 0;i < N;++i){
      int more = a[i] - mid;
      if(more > 0){
         sum += ceil(more + K - 1 / K);         //ceil
      }
   }

   return sum <= mid;
}

//二分时间
void solve(){
   int lb = -1,ub = INF;

   while(ub - lb > 1){
       int mid = (lb + ub) / 2;

       if(C(mid)) ub = mid;
       else lb = mid;
   }

   printf("%d\n",ub);
}

int main()
{
    while(~scanf("%d",&N)){
        for(int i = 0;i < N;++i)
            scanf("%d",&a[i]);

        scanf("%d",&K);
        if(K == 1){              //特判,防止分母为0
            printf("%d\n",*max_element(a,a+N));
            continue;
        }
        K--;
        solve();
    }
    return 0;
}



时间: 2024-12-14 13:59:43

POJ Drying的相关文章

poj 3104 Drying (二分)

/*设某次二分出的一个值是mid: 1.对于一件ai值小于等于mid的衣服,直接晾干即可: 2.对于一件ai值大于mid值的衣服,最少的用时是用机器一段时间, 晾干一段时间,设这两段时间分别是x1和x2, 那么有mid=x1+x2,ai<=k*x1+x2,解得x1>=(ai-mid)/(k-1) , 所以对(ai-mid)/(k-1)向上取整就是该件衣服的最少用时.*/ # include <stdio.h> # include <string.h> # include

POJ 3104 Drying(二分答案)

题目链接:http://poj.org/problem?id=3104 Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11128   Accepted: 2865 Description It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She is not afra

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

【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

POJ 3104 Drying

Drying Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 310464-bit integer IO format: %lld      Java class name: Main It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart g

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

传送门 表示又是神题一道 Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9327   Accepted: 2364 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 proces

POJ - 3104 :Drying

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 thi

POJ 3104 Drying(二分)

题目描述: Drying Time Limit: 2000MS Memory Limit: 65536K 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. B

POJ 3104 Drying(二分

Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22163   Accepted: 5611 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