Problem p02857 (python)

Fixed submission:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    ans = [-1]*(2*n)
    q = [i+1 for i in range(n)]
    que = ["?"]+q
    print(*que,flush = True)
    t = input()
    T = t
    l = 0
    r = n
    while r-l > 1:
        m = (l+r) >> 1
        q = [i+m+1 for i in range(n)]
        que = ["?"]+q
        print(*que,flush = True)
        t = input()
        if t == T:
            l = m
        else:
            r = m
    if T[0] == "R":
        ans[l] = "R"
        ans[l+n] = "B"
    else:
        ans[l] = "B"
        ans[l+n] = "R"
    Q = [i+r+1 for i in range(n-1)]
    for i in range(1,2*n+1):
        if ans[i-1] != -1:
            continue
        if i in Q:
            continue
        q = Q+[i]
        que = ["?"]+q
        print(*que,flush = True)
        t = input()
        if t[0] == "R":
            ans[i-1] = "R"
        else:
            ans[i-1] = "B"
    Q_ = []
    for i in range(2*n):
        if len(Q_) == (n>>1):
            break
        if ans[i] == "R" and i+1 not in Q:
            Q_.append(i+1)
    for i in range(2*n):
        if len(Q_) == n-1:
            break
        if ans[i] == "B" and i+1 not in Q:
            Q_.append(i+1)
    for i in Q:
        q = Q_+[i]
        que = ["?"]+q
        print(*que,flush = True)
        t = input()
        if t[0] == "R":
            ans[i-1] = "R"
        else:
            ans[i-1] = "B"
    ans = ["! "]+ans
    print(*ans,sep = "")
    return

#Solve
if __name__ == "__main__":
    solve()

Buggy submission (verdict: Runtime Error):
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    ans = [-1]*(2*n)
    q = [i+1 for i in range(n)]
    que = ["?"]+q
    print(*que,flush = True)
    t = input()
    T = t
    l = 0
    r = n
    while r-l > 1:
        m = (l+r) >> 1
        q = [i+m+1 for i in range(n)]
        que = ["?"]+q
        print(*que,flush = True)
        t = input()
        if t == T:
            l = m
        else:
            r = m
    if T[0] == "R":
        ans[l] = "R"
        ans[l+n] = "B"
    else:
        ans[l] = "B"
        ans[l+n] = "R"
    Q = [i for i in q[:-1]]
    for i in range(1,2*n+1):
        if i in Q:
            continue
        q = Q+[i]
        que = ["?"]+q
        print(*que,flush = True)
        t = input()
        if t[0] == "R":
            ans[i-1] = "R"
        else:
            ans[i-1] = "B"
    Q_ = []
    for i in range(2*n):
        if len(Q_) == (n>>1):
            break
        if ans[i] == "R" and i+1 not in Q:
            Q_.append(i+1)
    for i in range(2*n):
        if len(Q_) == n-1:
            break
        if ans[i] == "B" and i+1 not in Q:
            Q_.append(i+1)
    for i in Q:
        q = Q_+[i]
        que = ["?"]+q
        print(*que,flush = True)
        t = input()
        if t[0] == "R":
            ans[i-1] = "R"
        else:
            ans[i-1] = "B"
    ans = ["! "]+ans
    print(*ans,sep = "")
    return

#Solve
if __name__ == "__main__":
    solve()