-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuva10382.py
More file actions
65 lines (56 loc) · 1.6 KB
/
uva10382.py
File metadata and controls
65 lines (56 loc) · 1.6 KB
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
#!/usr/bin/env python3
# Watering Grass
# https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=15&page=show_problem&problem=1323
#
#
import os, sys
def main():
f = open('test') if 'TESTR' in os.environ else sys.stdin
i = 0
while True:
i += 1
line = f.readline()
if len(line) < 2:
return
n, l, w = map(int, line.split())
intervals = []
for _ in range(n):
c, r = map(int, f.readline().split())
intrvl = convertToInterval(c, r, w)
if intrvl:
intervals.append(intrvl)
print(findMinimum(intervals, l))
def convertToInterval(c, r, w):
if 2*r <= w:
return ()
from math import sqrt
h = sqrt(r*r - w * w/4)
return (c-h, c+h)
def findMinimum(intervals, l):
intervals.sort(key=lambda x : (x[0],-x[1]))
if len(intervals) == 0 or intervals[0][0] > 0: # return -1 if first sprinkler cannot cover the left end
return -1
intervals = [((0,y[1]) if y[0] <=0 else y) for y in intervals]
st = [(0,0)]
for i in range(len(intervals)):
x1, y1 = intervals[i]
x2, y2 = st[-1]
if y2 < x1:
return -1
if y1 > y2:
while len(st) > 1:
x2, y2 = st[-1]
x3, y3 = st[-2]
if y3 >= x1:
st.pop()
else:
break
st.append((x1,y1))
if y1 >= l:
break
if st[-1][1] < l:
return -1
else:
return len(st) - 1
if __name__ == "__main__":
main()