-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPROB beads.py
106 lines (79 loc) · 3.13 KB
/
PROB beads.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# -*- coding: utf-8 -*-
"""
ID: ion11
LANG: PYTHON3
PROB: beads
"""
f = open('beads.in','r')
fout = open('beads.out','w')
fin = f.readlines()
N = int(fin[0])
data = fin[1]
datalist = [bead for bead in data if bead == "w" or bead == "r" or bead == "b"]
best = 0
for i in range(N):
before_break = datalist.copy()
before_break.extend(datalist[:i])
after_break = datalist[i:]
after_break.extend(datalist)
after_break.reverse()
still_have_before = True
still_have_after = True
first_run = True
temp_best = 0
while still_have_before or still_have_after:
if first_run:
before_runner = before_break.pop()
after_runner = after_break.pop()
temp_best += 2
first_run = False
else:
if still_have_before:
before_next_one = before_break.pop()
if still_have_after:
after_next_one = after_break.pop()
if before_runner == "w" or after_runner == "w":
if before_runner == "w":
before_runner = before_next_one
temp_best += 1
else:
if (before_next_one == before_runner or before_next_one == "w") and still_have_before:
temp_best += 1
assert(still_have_before == True)
else:
still_have_before = False
assert(still_have_before == False)
if after_runner == "w":
after_runner = after_next_one
temp_best += 1
else:
if (after_next_one == after_runner or after_next_one == "w") and still_have_after:
temp_best += 1
assert(still_have_after == True)
else:
still_have_after = False
assert(still_have_after == False)
else:
if (before_next_one == before_runner or before_next_one == "w") and still_have_before:
temp_best += 1
assert(still_have_before == True)
else:
still_have_before = False
assert(still_have_before == False)
if (after_next_one == after_runner or after_next_one == "w") and still_have_after:
temp_best += 1
assert(still_have_after == True)
else:
still_have_after = False
assert(still_have_after == False)
if len(before_break) == 0:
still_have_before = False
assert(still_have_before == False)
if len(after_break) == 0:
still_have_after = False
assert(still_have_after == False)
if temp_best > best:
best = temp_best
if best > N:
best = N
fout.write(str(best)+"\n")