POJ 3104 Drying【二分】

用力戳我直达Virtual Judge~

题意:你有N件湿衣服,给你每一件衣服的潮湿度,衣服每min减少1潮湿度。你还有个吹风筒,吹风筒可以让衣服每min减少K个潮湿度。(最小单位是min,不能拆分)

做法:

1.如果k == 1,那么让自然干就行了,输出max(潮湿度);

2.二分结果,求左界。

a)潮湿度 <= mid的自然干

b)潮湿度 > mid的,根据k * x + (mid - x) >= cloth[i]求出x >= (cloth[i] - mid) / (k - 1),要向上取整,因为必须让它干。 sum += x,累计吹风筒花的时间。

c)如果sum > mid,则返回false,low = mid + 1;增加吹风时间以满足条件。

   如果sum <= mid,则返回true,high = mid;继续搜寻更小的吹风时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <stdio.h>
#include <math.h>
#define scf1(a) scanf("%d",&a)
using  namespace  std;
int  n, k;
int  cloth[100100];
bool  judge( int  mid){
     int  sum = 0;
     for ( int  i = 0; i < n; i++){
         if (cloth[i] <= mid)
             continue ;
         sum += (cloth[i] - mid) / (k - 1);    // k * x + (mid - x) >= cloth[i]
         if ( (cloth[i] - mid) % (k - 1) != 0)   //向上取整
             sum++;
         if (sum > mid)
             return  false ;
     }
     return  true ;
}
int  main(){
     scf1(n);
     int  low = 0, high = 0;
     for ( int  i = 0; i < n; i++){
         scf1(cloth[i]);
         high = max(high, cloth[i]);
     }
     scf1(k);
     if (k <= 1){
         printf ( "%d\n" , high);
         return  0;
     }
     while (low < high){
         int  mid = (low + high) >> 1;
         if (judge(mid)) high = mid;
         else  low = mid + 1;
     }
     printf ( "%d\n" ,high);
}

转载于:https://www.cnblogs.com/bestwzh/p/6478490.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值