from typing import Deque N = int(input()) lac = [list(map(str, input().split())) for _ in range(2 * N)] lac.sort(key=lambda x: int(x[0])) listR = Deque() listG = Deque() listB = Deque() for a, c in lac: if c == "R": listR.append(int(a)) elif c == "G": listG.append(int(a)) else: listB.append(int(a)) def curPop(cl, cr, lis): if len(lis) > 1 and lis[0] == cl: lis.popleft() lis.popleft() if len(lis) > 1 and lis[0] == cr: lis.pop() lis.pop() curL = int(lac[0][0]) curR = int(lac[-1][0]) while len(listR) + len(listG) + len(listB) > 4:
def __init__(self) -> None: self._container: Deque[T] = Deque()
def __init__(self): self.queue_in = Deque() self.queue_out = Deque()
def reverseLeftWords(s: str, n: int) -> str: queue = Deque(s) for i in range(n): queue.append(queue.popleft()) return "".join(list(queue))
def __init__(self): self._container = Deque()
async def run(self) -> None: loop = asyncio.get_event_loop() exhausted_boostables: List[Boostable[Any]] = [] not_ready_boostables: Deque[Boostable[Any]] = Deque() MIN_TIMEOUT = 0.01 MAX_TIMEOUT = 0.1 timeout = MIN_TIMEOUT if self.concurrency == 1: return while True: await self.semaphore.acquire() self.semaphore.release() while self.boostables: # We round robin the boostables until they're either all exhausted or not ready task = self.boostables[0].provide_boost() if isinstance(task, NotReady): not_ready_boostables.append(self.boostables.popleft()) continue if isinstance(task, Exhausted): exhausted_boostables.append(self.boostables.popleft()) continue assert isinstance(task, asyncio.Task) await asyncio.sleep(0) self.boostables.rotate(-1) if self.semaphore.locked(): break else: self.boostables = not_ready_boostables not_ready_boostables = Deque() if self.semaphore.locked(): # If we broke out of the inner loop due to a lack of available concurrency, go to # the top of the outer loop and wait for concurrency continue if self.shutdown and not self.boostables: # If we've been told to shutdown and we have nothing more to boost, exit break self.waiter = loop.create_future() try: # If we still have boostables (because they're currently not ready), timeout in # case they become ready # Otherwise, (since we have available concurrency), wait for us to be notified in # case of more work await asyncio.wait_for(self.waiter, timeout if self.boostables else None) # If we were waiting for boostables to become ready, increase how long we'd wait # the next time. Otherwise, reset the timeout duration. timeout = min(MAX_TIMEOUT, timeout * 2) if self.boostables else MIN_TIMEOUT except asyncio.TimeoutError: pass self.waiter = None # It's somewhat unintuitive that we can shutdown an executor while Boostables are still # being iterated over. So wait for them to finish as a courtesy (but not a guarantee). for boostable in exhausted_boostables: await boostable.wait() # Yield, so that iterations over boostables are likely to have finished by the time we exit # the BoostExecutor context await asyncio.sleep(0)
bx, by = map(int, input().split()) tx, ty = map(int, input().split()) from typing import Deque q = Deque() q.append([bx, by]) depth = [[0] * 9 for _ in range(9)] while q: cx, cy = q.popleft() if cx == tx and ty == cy: print(depth[cx][cy]) exit() for dx, dy in [[2, 1], [1, 2], [-1, -2], [-1, 2], [-2, -1], [2, -1], [-2, 1], [1, -2]]: nx, ny = dx + cx, cy + dy if nx > 0 and nx <= 8 and ny > 0 and ny <= 8 and depth[nx][ny] == 0: q.append([nx, ny]) depth[nx][ny] = depth[cx][cy] + 1