Recent Actions (Codeforces 1799A)
Tags: Greedy, data structures

On Codeforces the "Recent Actions" field shows the last $$$n$$$ posts with recent actions.Initially, there are posts $$$1, 2, \ldots, n$$$ in the field (this is in order from top to down). Also there are infinitely many posts not in the field, numbered with integers $$$n + 1, n + 2, \ldots$$$.When recent action happens in the post $$$p$$$:  If it is in the "Recent Actions" field, it moves from its position to the top position.  Otherwise, it is added to the top position, and the post on the down position is removed from the "Recent Actions" field. You know, that the next $$$m$$$ recent actions will happen in the posts $$$p_1, p_2, \ldots, p_m$$$ ($$$n + 1 \leq p_i \leq n + m$$$) in the moments of time $$$1, 2, \ldots, m$$$. Note, that recent actions only happen with posts with numbers $$$\geq n + 1$$$.For each post $$$i$$$ ($$$1 \leq i \leq n$$$), find the first time it will be removed from the "Recent Actions" field or say, that it won't be removed.

Input: The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Descriptions of test cases follow.The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n, m \leq 5 \cdot 10^4$$$) — the size of the "Recent Actions" field and the number of actions.The next line contains $$$m$$$ integers $$$p_1, p_2, \ldots, p_m$$$ ($$$n + 1 \leq p_i \leq n + m$$$).It is guaranteed, that the sum of $$$n$$$ and the sum of $$$m$$$ for all test cases does not exceed $$$5 \cdot 10^4$$$.

Output: For each test case print $$$n$$$ integers $$$t_1, t_2, \ldots, t_n$$$, where $$$t_i=-1$$$ if the post $$$i$$$ won't be removed or $$$t_i$$$ equals to the first moment of time the post $$$i$$$ will be removed ($$$1 \leq t_i \leq m$$$).

Note: In the first test case, the only post $$$1$$$ will be removed at the moment $$$1$$$ and replaced by the post $$$2$$$.In the second test case the "Recent Actions" field will be (given an order from top to down):  Before moment $$$1$$$: $$$[1, 2, 3]$$$, after moment $$$1$$$: $$$[5, 1, 2]$$$. Post number $$$3$$$ was removed.  Before moment $$$2$$$: $$$[5, 1, 2]$$$, after moment $$$2$$$: $$$[4, 5, 1]$$$. Post number $$$2$$$ was removed. Post number $$$1$$$ won't be removed.In the third test case the "Recent Actions" field will be (given an order from top to down):  Before moment $$$1$$$: $$$[1, 2, 3, 4]$$$, after moment $$$1$$$: $$$[5, 1, 2, 3]$$$. Post number $$$4$$$ was removed.  Before moment $$$2$$$: $$$[5, 1, 2, 3]$$$, after moment $$$2$$$: $$$[9, 5, 1, 2]$$$. Post number $$$3$$$ was removed.  Before moment $$$3$$$: $$$[9, 5, 1, 2]$$$, after moment $$$3$$$: $$$[9, 5, 1, 2]$$$. Nothing was changed.  Before moment $$$4$$$: $$$[9, 5, 1, 2]$$$, after moment $$$4$$$: $$$[5, 9, 1, 2]$$$. The order was changed.  Before moment $$$5$$$: $$$[5, 9, 1, 2]$$$, after moment $$$5$$$: $$$[7, 5, 9, 1]$$$. Post number $$$2$$$ was removed. Post number $$$1$$$ won't be removed.

Sample Inputs:
10
1 1
2
3 2
5 4
4 5
5 9 9 5 7
5 5
6 7 8 9 10
3 4
4 4 4 4
4 4
5 5 6 6
3 5
4 5 5 5 4
4 20
5 5 24 24 24 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20
5 7
7 8 7 11 7 12 10
6 7
8 11 7 8 8 8 12

Sample Outputs:
1 
-1 2 1 
-1 5 2 1 
5 4 3 2 1 
-1 -1 1 
-1 -1 3 1 
-1 2 1 
8 7 3 1 
7 6 4 2 1 
-1 -1 7 3 2 1

Solution (Python):
import sys
input = sys.stdin.readline

def inp():
    return int(input())

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

def insr():
    s = input()
    return list(s[:len(s) - 1])

def invr():
    return map(int, input().split())
test_cases = inp()
inp_arr = []
for i in range(test_cases):
    nums = inlt()
    (n, m) = (nums[0], nums[1])
    ms = inlt()
    inp_arr.append((n, ms))

def findResult(n, ms):
    result = [-1] * n
    curPos = n - 1
    cur = 0
    d = {}
    for m in ms:
        cur += 1
        if m in d:
            continue
        else:
            d[m] = 1
            result[curPos] = cur
            curPos -= 1
            if curPos < 0:
                break
    return ' '.join([str(x) for x in result])
result = [findResult(n, ms) for (n, ms) in inp_arr]
for m in result:
    print(m)

Solution (Python):
def i():
    (n, m) = map(int, input().split())
    s = [0, *list(map(int, input().split()))]
    c = ['-1'] * (n + m + 1)
    z = n
    for x in range(1, m + 1):
        if c[s[x]] == '-1':
            c[s[x]] = '1'
            c[z] = str(x)
            z -= 1
    return ' '.join(c[1:n + 1])
t = int(input())
a = []
for _ in range(t):
    a.append(i())
print('\n'.join(a))

Solution (Python):
k = int(input())
final = []
while k > 0:
    arr = []
    x = input().split()
    n = int(x[0])
    m = int(x[1])
    x = input().split()
    for num in x:
        arr.append(int(num))
    res = [-1] * n
    index = n - 1
    cur = 0
    added = set()
    for num in arr:
        cur += 1
        if num in added:
            continue
        added.add(num)
        res[index] = cur
        index -= 1
        if index < 0:
            break
    final.append(res)
    k -= 1
for x in final:
    s = ''
    for (i, num) in enumerate(x):
        if i < len(x) - 1:
            s += str(num) + ' '
        else:
            s += str(num)
    print(s)