Keep it Beautiful (Codeforces 1841B)
Tags: Implement, implementation

The array $$$[a_1, a_2, \dots, a_k]$$$ is called beautiful if it is possible to remove several (maybe zero) elements from the beginning of the array and insert all these elements to the back of the array in the same order in such a way that the resulting array is sorted in non-descending order.In other words, the array $$$[a_1, a_2, \dots, a_k]$$$ is beautiful if there exists an integer $$$i \in [0, k-1]$$$ such that the array $$$[a_{i+1}, a_{i+2}, \dots, a_{k-1}, a_k, a_1, a_2, \dots, a_i]$$$ is sorted in non-descending order.For example:  $$$[3, 7, 7, 9, 2, 3]$$$ is beautiful: we can remove four first elements and insert them to the back in the same order, and we get the array $$$[2, 3, 3, 7, 7, 9]$$$, which is sorted in non-descending order;  $$$[1, 2, 3, 4, 5]$$$ is beautiful: we can remove zero first elements and insert them to the back, and we get the array $$$[1, 2, 3, 4, 5]$$$, which is sorted in non-descending order;  $$$[5, 2, 2, 1]$$$ is not beautiful. Note that any array consisting of zero elements or one element is beautiful.You are given an array $$$a$$$, which is initially empty. You have to process $$$q$$$ queries to it. During the $$$i$$$-th query, you will be given one integer $$$x_i$$$, and you have to do the following:  if you can append the integer $$$x_i$$$ to the back of the array $$$a$$$ so that the array $$$a$$$ stays beautiful, you have to append it;  otherwise, do nothing. After each query, report whether you appended the given integer $$$x_i$$$, or not.

Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.Each test case consists of two lines. The first line contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. The second line contains $$$q$$$ integers $$$x_1, x_2, \dots, x_q$$$ ($$$0 \le x_i \le 10^9$$$).Additional constraint on the input: the sum of $$$q$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$).

Output: For each test case, print one string consisting of exactly $$$q$$$ characters. The $$$i$$$-th character of the string should be 1 if you appended the integer during the $$$i$$$-th query; otherwise, it should be 0.

Note: Consider the first test case of the example. Initially, the array is $$$[]$$$.  trying to append an integer $$$3$$$. The array $$$[3]$$$ is beautiful, so we append $$$3$$$;  trying to append an integer $$$7$$$. The array $$$[3, 7]$$$ is beautiful, so we append $$$7$$$;  trying to append an integer $$$7$$$. The array $$$[3, 7, 7]$$$ is beautiful, so we append $$$7$$$;  trying to append an integer $$$9$$$. The array $$$[3, 7, 7, 9]$$$ is beautiful, so we append $$$9$$$;  trying to append an integer $$$2$$$. The array $$$[3, 7, 7, 9, 2]$$$ is beautiful, so we append $$$2$$$;  trying to append an integer $$$4$$$. The array $$$[3, 7, 7, 9, 2, 4]$$$ is not beautiful, so we don't append $$$4$$$;  trying to append an integer $$$6$$$. The array $$$[3, 7, 7, 9, 2, 6]$$$ is not beautiful, so we don't append $$$6$$$;  trying to append an integer $$$3$$$. The array $$$[3, 7, 7, 9, 2, 3]$$$ is beautiful, so we append $$$3$$$;  trying to append an integer $$$4$$$. The array $$$[3, 7, 7, 9, 2, 3, 4]$$$ is not beautiful, so we don't append $$$4$$$. 

Sample Inputs:
3
9
3 7 7 9 2 4 6 3 4
5
1 1 1 1 1
5
3 2 1 2 3

Sample Outputs:
111110010
11111
11011

Solution (Python):
import sys

class codeforces(object):

    def Solution(self, data):
        small = False
        curr = data[0]
        ans = []
        for n in data:
            if not small:
                if n < curr:
                    if n <= data[0]:
                        small = True
                        ans.append('1')
                        curr = n
                    else:
                        ans.append('0')
                else:
                    ans.append('1')
                    curr = n
            elif curr <= n <= data[0]:
                ans.append('1')
                curr = n
            else:
                ans.append('0')
        return ''.join(ans)
codeforces = codeforces()
ans = []
tests = int(sys.stdin.readline())
for i in range(tests):
    queries = int(sys.stdin.readline())
    data = sys.stdin.readline().split()
    data = [int(i) for i in data]
    ans.append(codeforces.Solution(data))
for a in ans:
    print(a)

Solution (Python):
def solve():
    n = int(input())
    mp = list(map(int, input().split()))
    flag = False
    maxN = mp[0]
    ans = '1'
    for i in range(1, n):
        if flag == False:
            if mp[i] >= maxN:
                maxN = mp[i]
                ans += '1'
            elif mp[i] <= mp[0]:
                ans += '1'
                maxN = mp[0]
                minN = mp[i]
                flag = True
            else:
                ans += '0'
        elif mp[i] <= maxN and mp[i] >= minN:
            ans += '1'
            minN = mp[i]
        else:
            ans += '0'
    pri.append(ans)
pri = []
for _ in range(int(input())):
    solve()
print(*pri, sep='\n')

Solution (Python):
import os
import sys
import math
from io import BytesIO, IOBase
from collections import deque, defaultdict, OrderedDict, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, insort, bisect_left
from functools import lru_cache
from itertools import permutations, combinations
sys.setrecursionlimit(10 ** 6)

def STRIN():
    return input()

def INTIN():
    return int(input())

def LINT():
    return list(map(int, input().split()))

def LSTR():
    return list(map(str, input().split()))

def MINT():
    return map(int, input().split())

def MSTR():
    return map(str, input().split())

def divisors(n):
    c = 0
    i = 1
    while i <= int(math.sqrt(n)):
        if n % i == 0:
            if n // i == i:
                c += 1
            else:
                c += 2
        i = i + 1
    return c

def isValid(x, y, n, m, graph, vis):
    if x < 0 or x >= n or y < 0 or (y >= m) or (graph[x][y] == '#') or vis[x][y]:
        return False
    return True

def dfs(u, graph, vis, parent, p, vertex):
    vis[u] = 1
    parent[u] = p
    for v in graph[u]:
        if vis[v]:
            if p != v:
                vertex[0] = v
                vertex[1] = u
                return True
        if not vis[v]:
            if dfs(v, graph, vis, parent, u, vertex):
                return True
    return False

def bfs(u, graph, vis, teams):
    pass

def palin(s):
    s = str(s)
    i = 0
    j = len(s) - 1
    while i <= j:
        if s[i] != s[j]:
            return False
        i += 1
        j -= 1
    return True

def solve():
    n = int(input())
    l = list(map(int, input().split()))
    end = l[0]
    start = l[0]
    res = '1'
    cnt = 0
    for i in range(1, n):
        if l[i] >= end and cnt == 0:
            end = l[i]
            res += '1'
        elif start >= l[i] and cnt == 0:
            end = l[i]
            res += '1'
            cnt += 1
        elif l[i] >= end and l[i] <= start:
            end = l[i]
            res += '1'
        else:
            res += '0'
    print(res)

def main():
    ans = []
    letter = 'abcdefghijklmnopqrstuvwxyz'
    for i in letter:
        ans.append(i)
    for i in letter:
        for j in letter:
            ans.append(i + j)
    for i in letter:
        for j in letter:
            for k in letter:
                ans.append(i + j + k)
    ans = {}
    k = 1
    for i in range(97, 124):
        ans[str(k)] = chr(i)
        k += 1
    for _ in range(INTIN()):
        solve()
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')
if __name__ == '__main__':
    main()