I was trying to solve this problem but was running out of time on testcases 10 and 20, so I decided to try the offical solution here but this one also was running out of time. Is there something that Im missing?
In these two testcases you need to output an insane amount of data (more than 7mb on a textfile), and that’s probably what is making both solutions fail.
I also noticed one strange behavior (I don’t know if I should open another post for this but it’s for the same problem): on inputs 11 and 21 my code outputs the same exact code as the one of the solution and the one in the solutions, but it still count it as wrong (I already checked and I don’t print extra space at the end of each line, the output of the programs are exactly the same characters)
Btw, the Python code runs fast enough if everything is concatenated before being printed:
Code
import sys
def to_reduced_list(s):
"""Compress runs of equal chars in a string s, and converts to int
>>> to_reduced_list('2211')
[2, 1]
"""
l = []
for c in s:
c = int(c)
if len(l) > 0 and l[-1] == c:
continue
l.append(c)
return l
output = []
def solve():
N, P = map(int, input().split())
tubes = [to_reduced_list(input()) for _ in range(2)]
tubes.append([])
if tubes[0][0] == tubes[1][0]: # ensure f and s start with different chars
tubes[0].insert(0, tubes[0][0] ^ 3)
ans = len(tubes[0]) + len(tubes[1]) - 2
if ans > 1:
ans += 1
output.append(f"{ans}")
if P == 1:
return
moves = []
def move(src, dst):
moves.append((src, dst))
if len(tubes[dst]) == 0 or tubes[dst][-1] != tubes[src][-1]:
tubes[dst].append(tubes[src][-1])
tubes[src].pop()
if tubes[0][-1] == tubes[1][-1]: # step 1: if equal last chars
if len(tubes[0]) > len(tubes[1]):
move(0, 1)
else:
move(1, 0)
for i in range(2):
if len(tubes[i]) > 1:
move(i, 2) # step 2: move from any tube with string length > 1 to beaker
idx_to_empty = 0 # step 3: choose a tube to (almost) empty first
if tubes[idx_to_empty][0] == tubes[2][0]:
idx_to_empty ^= 1
while len(tubes[idx_to_empty]) > 1:
if tubes[idx_to_empty][-1] == tubes[2][0]:
move(idx_to_empty, 2)
else:
move(idx_to_empty, idx_to_empty ^ 1)
idx_to_empty ^= 1 # step 4: next, (almost) empty the other tube
while len(tubes[idx_to_empty]) > 1:
if tubes[idx_to_empty][-1] == tubes[2][0]:
move(idx_to_empty, 2)
else:
move(idx_to_empty, idx_to_empty ^ 1)
move(2, idx_to_empty) # step 5: finish
break
assert len(moves) == ans
for a, b in moves:
output.append(f"{1 + a} {1 + b}")
T = int(input())
for _ in range(T):
solve()
print("\n".join(output))