blob: 9f574da155fb37b7bce5aa8b005ead5129184d6c (
plain)
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
|
f = open('3.in', 'r')
o = open('3.out', 'w')
T = int(f.readline().strip())
for t in xrange(T):
line = f.readline().strip().split(' ')[1:]
prs = zip(line[::2], map(int,line[1::2]))
oprs = [x[1] for x in prs if x[0]=='O']
bprs = [x[1] for x in prs if x[0]=='B']
op, bp = 1, 1
step = 0
while (prs):
step += 1
#print prs, oprs, bprs, op, bp
pushed = False
if oprs:
if prs[0] == ('O', op):
oprs = oprs[1:]
prs = prs[1:]
pushed = True
elif oprs[0] > op:
op += 1
elif oprs[0] < op:
op -= 1
if bprs:
if prs[0] == ('B', bp) and not pushed:
bprs = bprs[1:]
prs = prs[1:]
elif bprs[0] > bp:
bp += 1
elif bprs[0] < bp:
bp -= 1
res = step
s = "Case #%d: %d\n" % (t+1, res)
#print s
o.write(s)
f.close()
o.close()
|