-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCHEDULE.py
65 lines (57 loc) · 1.56 KB
/
SCHEDULE.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
def getbig(A):
size = 1
loc = 0
count = 0
pre = -1
for i in range(len(A)):
if i == 0:
pre = A[i]
count = 1
loc = i
size = 1
continue
if pre == A[i]:
count += 1
if count > size :
size = count
loc = i
else:
count = 1
pre = A[i]
return (size, loc)
for _ in range(int(raw_input())):
N, K = map(int, raw_input().split())
G1 = [1 if i % 2 == 0 else 0 for i in range(N)]
G2 = [0 if i % 2 == 0 else 1 for i in range(N)]
seq = map(int, raw_input().replace('', ' ').split())
dif1 = [a-b for a,b in zip(G1, seq)]
dif2 = [a-b for a,b in zip(G2, seq)]
if dif1.count(1) + dif1.count(-1) <= K or dif2.count(1) + dif2.count(-1) <= K:
print 1
continue
ans = N
for _ in range(K):
big, loc = getbig(seq)
#print "Big Loc = ",big, loc
if ans > big:
ans = big
loc = loc - big + 1
if big % 2 == 0:
temp1 = loc + big/2 - 1
temp2 = loc + big/2
if N-1-temp2 < temp1:
loc = temp2
else:
loc = temp1
else:
loc = loc + big/2
if seq[loc] == 1:
seq[loc] = 0
else:
seq[loc] = 1
#print "Seq=", seq
big, loc = getbig(seq)
#print "out Big Loc = ",big, loc
if ans > big:
ans = big
print ans