Game Master (Codeforces 1608C)
Tags: Greedy, two pointers

$$$n$$$ players are playing a game. There are two different maps in the game. For each player, we know his strength on each map. When two players fight on a specific map, the player with higher strength on that map always wins. No two players have the same strength on the same map. You are the game master and want to organize a tournament. There will be a total of $$$n-1$$$ battles. While there is more than one player in the tournament, choose any map and any two remaining players to fight on it. The player who loses will be eliminated from the tournament. In the end, exactly one player will remain, and he is declared the winner of the tournament. For each player determine if he can win the tournament.

Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The description of test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of players.The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$, $$$a_i \neq a_j$$$ for $$$i \neq j$$$), where $$$a_i$$$ is the strength of the $$$i$$$-th player on the first map. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \leq b_i \leq 10^9$$$, $$$b_i \neq b_j$$$ for $$$i \neq j$$$), where $$$b_i$$$ is the strength of the $$$i$$$-th player on the second map. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

Output: For each test case print a string of length $$$n$$$. $$$i$$$-th character should be "1" if the $$$i$$$-th player can win the tournament, or "0" otherwise.

Note: In the first test case, the $$$4$$$-th player will beat any other player on any game, so he will definitely win the tournament.In the second test case, everyone can be a winner. In the third test case, there is only one player. Clearly, he will win the tournament.

Sample Inputs:
3
4
1 2 3 4
1 2 3 4
4
11 12 20 21
44 22 11 30
1
1000000000
1000000000

Sample Outputs:
0001
1111
1

Solution (Python):
import itertools
import math
import os
import random
import sys
from bisect import bisect, bisect_left
from collections import *
from functools import reduce
from heapq import heapify, heappop, heappush
from io import BytesIO, IOBase
from string import *
BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = 'x' in file.mode or 'r' not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b'\n') + (not b)
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            (self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode('ascii'))
        self.read = lambda : self.buffer.read().decode('ascii')
        self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
ints = lambda : list(map(int, input().split()))

def printQry(a, b) -> None:
    sa = str(a)
    sb = str(b)
    print(f'? {sa} {sb}', flush=True)

def printAns(ans) -> None:
    s = str(ans)
    print(f'! {s}', flush=True)

def solve() -> None:
    n = int(input())
    A = ints()
    B = ints()
    ans = [0] * n
    idx1 = sorted(range(n), key=lambda x: -A[x])
    d = {v: i for (i, v) in enumerate(idx1)}
    idx2 = sorted(range(n), key=lambda x: -B[x])
    last = p = 0
    for i in range(n):
        if d[idx2[i]] <= p:
            p = last
        last = max(last, d[idx2[i]])
    for i in range(p + 1):
        ans[idx1[i]] = 1
    print(*ans, sep='')
for _ in range(int(input())):
    solve()

Solution (Python):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def game_master(n, a, b):
    a_indices = sorted(range(n), key=lambda x: a[x])
    b_indices = sorted(range(n), key=lambda x: b[x])
    pos_by_a_index = [None] * n
    for (pos, ai_) in enumerate(a_indices):
        pos_by_a_index[ai_] = pos
    pos_by_b_index = [None] * n
    for (pos, bi_) in enumerate(b_indices):
        pos_by_b_index[bi_] = pos
    output = [0] * n
    hi_a_pos = n
    hi_b_pos = n
    low_a_pos = n - 1
    while True:
        lowest_b_position = n
        for a_pos in range(low_a_pos, hi_a_pos):
            ai = a_indices[a_pos]
            output[ai] = 1
            lowest_b_position = min(lowest_b_position, pos_by_b_index[ai])
        hi_a_pos = low_a_pos
        lowest_a_position = low_a_pos
        for b_pos in range(lowest_b_position, hi_b_pos):
            bi = b_indices[b_pos]
            output[bi] = 1
            lowest_a_position = min(lowest_a_position, pos_by_a_index[bi])
        hi_b_pos = lowest_b_position
        if lowest_a_position == low_a_pos:
            break
        low_a_pos = lowest_a_position
    return ''.join(map(str, output))
for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    sys.stdout.write(f'{game_master(n, a, b)}\n')

Solution (Python):
from collections import deque

def solution(n, map1, map2):
    rank1 = sorted(range(n), key=lambda x: map1[x])
    rank2 = sorted(range(n), key=lambda x: map2[x])
    map1_popped = {*()}
    map2_popped = {*()}
    b1 = rank1.pop()
    b2 = rank2.pop()
    queue = deque([[b1, 1], [b2, 2]])
    rst = [0] * n
    map1_popped.add(b1)
    map2_popped.add(b2)
    rst[b1] = 1
    rst[b2] = 1
    while queue:
        [p, hyx_from] = queue.popleft()
        if hyx_from == 1:
            while not p in map2_popped:
                better2 = rank2.pop()
                map2_popped.add(better2)
                if better2 == p:
                    break
                    _hy_anon_var_1 = None
                else:
                    _hy_anon_var_1 = None
                queue.append([better2, 2])
                rst[better2] = 1
            _hy_anon_var_3 = None
        else:
            while not p in map1_popped:
                better1 = rank1.pop()
                map1_popped.add(better1)
                if better1 == p:
                    break
                    _hy_anon_var_2 = None
                else:
                    _hy_anon_var_2 = None
                queue.append([better1, 1])
                rst[better1] = 1
            _hy_anon_var_3 = None
    return rst
t = int(input())
for _ in range(t):
    n = int(input())
    map1 = list(map(int, input().split()))
    map2 = list(map(int, input().split()))
    print(''.join(map(str, solution(n, map1, map2))))