题意:你有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);
}
|