0%

二分法.md

扫地机器人

题目:https://www.lanqiao.cn/problems/199/learning/

  • 每个机器人“负责”的区域(包括他自己的)*2=他的时间
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# n, k = map(int, input().split())
# # n, k = 10, 3
# arr = [False] * (n + 1)
#
# for i in range(k):
# arr[int(input())] = True
#
#
# # for i in [5, 2, 10]:
# # arr[i] = True
#
#
# def check(d):
# i = 1
# while i <= n:
# # print(d, i, i + d, arr[i:i + d])
# if not any(arr[i:i + d]):
# return False
# i += d
# return True
#
#
# l, r = 1, n
# while l < r:
# mid = (l + r) // 2
# print(l, r, mid)
# if check(mid):
# r = mid
# else:
# l = mid + 1
# print((l - 1) * 2)


n, k = map(int, input().split())
a = [0]
for i in range(k):
a.append(int(input()))
a.sort()

print(a)


def check(d):
len = 0 # 覆盖长度,从最左端开始一直到最右端
for i in range(1, k + 1):
if len + d >= a[i]: # 当前覆盖长度加上d之后,是否包括了a[i]
if len > a[i]: # 当前覆盖长度是否已经包括了a[i]
# 如果当前覆盖长度已经包括了,说明a[i]之前的机器人已经负责了到len这块的区域
# 那a[i]就可以负责len往后再加d的部分
len = a[i] + d - 1 # 减1是因为还要算上自己
else:
# 如果当前覆盖长度没有包括a[i],那就让a[i]负责[len+1,len+d]
len += d
else:
return False
return len >= n


l, r = 1, n
mid = 0
while l < r:
mid = (l + r) // 2
print(l, r, mid)
if check(mid):
r = mid
else:
l = mid + 1
print((l - 1) * 2)