Hi! This is my code for the Swapity Swap question with python.
import sys sys.stdin = open('swap.in', 'r') sys.stdout = open('swap.out', 'w') N, K = map(int, input().split()) A1, A2 = map(int, input().split()) B1, B2 = map(int, input().split()) original = [j for j in range(1, N+1)] cow_numbering = [i for i in range(1, N+1)] same = False count = 0 while not same: cow_numbering[A1 - 1: A2] = cow_numbering[A2 - 1: A1 - 2: -1] cow_numbering[B1 - 1: B2] = cow_numbering[B2 - 1: B1 - 2: -1] count += 1 if cow_numbering == original: same = True for j in range(K % count): cow_numbering[A1-1: A2] = cow_numbering[A2-1: A1-2: -1] cow_numbering[B1-1: B2] = cow_numbering[B2-1: B1-2: -1] for i in cow_numbering: print(i)
Turns out that this works for 9/13 test cases (misses 5,6,9,11) due to time error. Obviously based on the constraints described in the problem, these time errors don’t occur due to the actual values being too much.
Can someone tell me the reason for this and tell me how to fix it?