T-shirt buying (Codeforces 799B)
Tags: Implement, data structures

A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers pi, ai and bi, where pi is the price of the i-th t-shirt, ai is front color of the i-th t-shirt and bi is back color of the i-th t-shirt. All values pi are distinct, and values ai and bi are integers from 1 to 3.m buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the j-th buyer we know his favorite color cj.A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.You are to compute the prices each buyer will pay for t-shirts.

Input: The first line contains single integer n (1 ≤ n ≤ 200 000) — the number of t-shirts.The following line contains sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ 1 000 000 000), where pi equals to the price of the i-th t-shirt.The following line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 3), where ai equals to the front color of the i-th t-shirt.The following line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 3), where bi equals to the back color of the i-th t-shirt.The next line contains single integer m (1 ≤ m ≤ 200 000) — the number of buyers. The following line contains sequence c1, c2, ..., cm (1 ≤ cj ≤ 3), where cj equals to the favorite color of the j-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served. 

Output: Print to the first line m integers — the j-th integer should be equal to the price of the t-shirt which the j-th buyer will buy. If the j-th buyer won't buy anything, print -1.


Sample Inputs:
5
300 200 400 500 911
1 2 1 2 3
2 1 3 2 1
6
2 3 1 2 1 1

2
1000000000 1
1 1
1 2
2
2 1

Sample Outputs:
200 400 300 500 911 -1

1 1000000000

Solution (Python):
import os, sys, math, itertools
from collections import deque, defaultdict, OrderedDict, Counter
from bisect import bisect, bisect_left, bisect_right, insort
from heapq import heapify, heappush, heappop, nsmallest, nlargest, heapreplace, heappushpop
ii = lambda : int(input())
si = lambda : input()
mi = lambda : map(int, input().strip().split(' '))
msi = lambda : map(str, input().strip().split(' '))
li = lambda : list(mi())
lsi = lambda : list(msi())
out = []
export = lambda : print('\n'.join(map(str, out)), end='')
p = lambda x: out.append(x)
pp = lambda array: p(' '.join(map(str, array)))
offline = True
L = lambda string: hq.L(string) if offline else False
LT = lambda tc, custom, string: hq.LT(tc, custom, string) if offline else False

def main() -> None:
    n = ii()
    p = mi()
    a = mi()
    b = mi()
    input()
    c = mi()
    t = sorted(zip(p, a, b), key=lambda x: x[0])
    p = [None, 0, 0, 0]
    r = []
    for ci in c:
        i = p[ci]
        while i < n and (t[i] is None or ci not in t[i][1:]):
            i += 1
        if i == n:
            r.append('-1')
        else:
            r.append(str(t[i][0]))
            t[i] = None
            i += 1
        p[ci] = i
    print(' '.join(r))
try:
    exec('from hq import *\nexec(cc)')
except (FileNotFoundError, ModuleNotFoundError):
    offline >> 1
    main()
    export()

Solution (Python):
import operator
n = int(input())
p = map(int, input().split())
a = map(int, input().split())
b = map(int, input().split())
input()
c = map(int, input().split())
t = sorted(zip(p, a, b), key=operator.itemgetter(0))
p = [None, 0, 0, 0]
r = []
for ci in c:
    i = p[ci]
    while i < n and (t[i] is None or ci not in t[i][1:]):
        i += 1
    if i == n:
        r.append('-1')
    else:
        r.append(str(t[i][0]))
        t[i] = None
        i += 1
    p[ci] = i
print(' '.join(r))

Solution (Python):
from collections import deque, defaultdict
n = int(input())
p = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
m = int(input())
c = list(map(int, input().split(' ')))
result = list()
colors_1 = defaultdict(list)
for i in range(n):
    colors_1[a[i]].append(p[i])
    colors_1[b[i]].append(p[i])
for i in range(1, 4):
    colors_1[i].sort()
    colors_1[i] = deque(colors_1[i])
buyed = defaultdict(lambda : 0)
for q in c:
    while True:
        try:
            pr = colors_1[q].popleft()
        except IndexError:
            pr = -1
            break
        else:
            if buyed[pr] == 0:
                break
    result.append(pr)
    buyed[pr] = 1
result = list(map(str, result))
print(' '.join(result))