Problem:


  MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Problem A
Rearranging a Sequence

You are given an ordered sequence of integers, ($1, 2, 3, ..., n$). Then, a number of requests will be given. Each request specifies an integer in the sequence. You need to move the specified integer to the head of the sequence, leaving the order of the rest untouched. Your task is to find the order of the elements in the sequence after following all the requests successively.

Input

The input consists of a single test case of the following form.

$n$ $m$

$e_1$

...

$e_m$

The integer $n$ is the length of the sequence ($1 \leq n \leq 200000$). The integer $m$ is the number of requests ($1 \leq m \leq 100000$). The following $m$ lines are the requests, namely $e_1, ...,  e_m$, one per line. Each request $e_i$ ($1 \leq i \leq m$) is an integer between 1 and $n$, inclusive, designating the element to move. Note that, the integers designate the integers themselves to move, not their positions in the sequence.

Output

Output the sequence after processing all the requests. Its elements are to be output, one per line, in the order in the sequence.

Sample Input 1

5 3
4
2
5

Sample Output 1

5
2
4
1
3

Sample Input 2

10 8
1
4
7
3
4
10
1
3

Sample Output 2

3
1
10
4
7
2
5
6
8
9

In Sample Input 1, the initial sequence is (1, 2, 3, 4, 5). The first request is to move the integer 4 to the head, that is, to change the sequence to (4, 1, 2, 3, 5). The next request to move the integer 2 to the head makes the sequence (2, 4, 1, 3, 5). Finally, 5 is moved to the head, resulting in (5, 2, 4, 1, 3).



Sample input:
5 3
4
2
5


Sample output:
5
2
4
1
3

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n, m = map(int, input().split())
e = [int(input()) for _ in range(m)]
d = deque(i for i in range(1, n+1))
for v in e[::-1]:
    try:
        d.remove(v)
        print(v)
    except ValueError:
        pass
while d:
    print(d.popleft())

Submission status: Time Limit Exceeded

Solution code:
[n,m] = map(int,input().split())

t = [ i + 1 for i in range(n) ]

for _ in range(m):
    e = int(input())
    t.remove(e)
    t.insert(0,e)

for i in t:
    print(i)

Submission status: Accepted

Solution code:
#!/usr/bin/env python

import sys
import math
import itertools as it
from collections import deque

sys.setrecursionlimit(10000000)

n, m = map(int, raw_input().split())

lst = []
for i in range(m):
    lst.append(input())

lst = list(reversed(lst))
flag = [True for i in range(n + 1)]

for num in lst:
    if flag[num]:
        print num
        flag[num] = False

for i in range(1, n + 1):
    if flag[i]:
        print i

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a=[0]*(n+1);
b=[int(input()) for _ in range(m)]
for x in b[::-1]:
    if a[x]==0:a[x]=1;print(x)
[print(i)for i in range(1,n+1)if a[i]==0]

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
e = [int(input()) for _ in range(m)]
b = [1] * (n+1)
for v in e[::-1]:
    if b[v]:
        b[v] = 0
        print(v)
for i in range(1, n+1):
    if b[i]:
        print(i)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
E = [int(input()) for i in range(m)]
s = set()
E.reverse()

F = []
for e in E:
    if e in s:
        continue
    s.add(e)
    F.append(e)
for i in range(1, n+1):
    if i in s:
        continue
    F.append(i)
*_,=map(print,F)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
used = [1] + [0] * N
src = [int(input()) for i in range(M)]
for v in reversed(src):
    if used[v]: continue
    print(v)
    used[v] = 1
for v,u in enumerate(used):
    if not u: print(v)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    while True:
        n,m = LI()
        a = [I() for _ in range(m)][::-1]
        f = [None] * (n+1)
        for c in a:
            if f[c] is None:
                f[c] = 1
                rr.append(c)

        for c in range(1,n+1):
            if f[c] is None:
                rr.append(c)

        break



    return '\n'.join(map(str, rr))


print(main())

Submission status: Accepted

Solution code:
n, m =  map(int, input().split())
e = [int(input()) for i in range(m)]
used = [False]*n
e.reverse()
ans = []
for x in e:
    if not used[x-1]:
        ans.append(x)
        used[x-1] = True
for i in range(n):
    if not used[i]:
        ans.append(i+1)
for x in ans:
    print(x)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from collections import deque
input = lambda: sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**8)
inf = float('inf')
mod = 10**9+7

ans=deque()
used=[0]*(10**6)
n,m=map(int,input().split())
data=reversed([int(input()) for i in range(m)])
for  di in data:
    if not used[di]:
        ans.append(di)
    used[di]=1
plusans=[i for i in range(1,n+1) if not  used[i]]
ans+=plusans
for i in ans:
    print(i)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a=[]
for i in range(n):
	a.append(i)
for i in range(m):
	e=int(input())
	a[e-1]=-i-1
b=[]
for i in range(n):
	b.append([a[i],i+1])
b.sort()
for i in range(n):
	print(b[i][1])

Submission status: Accepted

Solution code:

N,M = map(int,input().split())

lis = [False] * N
end = [False] * N
aft = []
for i in range(M):

    e = int(input())

    aft.append(e)
    lis[e-1] = True

aft.reverse()

for i in aft:

    if not end[i-1]:
        print (i)
        end[i-1] = True

for i in range(N):

    if not lis[i]:
        print (i+1)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
query = [int(input()) for _ in range(M)]
ans = []
set_ = set()
for q in query[::-1]:
    if q not in set_:
        ans.append(q)
        set_.add(q)
for i in range(1,N+1):
    if i not in set_:
        ans.append(i)
print(*ans,sep="\n")

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    n,m = LI()
    e = IR(m)
    f = [0 for i in range(n+1)]
    ans = [None for i in range(n)]
    i = m-1
    while i >= 0:
        if not f[e[i]]:
            f[e[i]] = 1
        else:
            e.pop(i)
        i -= 1
    ans[:len(e)] = e[::-1]
    k = sum(f)
    for i in range(1,n+1):
        if not f[i]:
            ans[k] = i
            k += 1
    for i in ans:
        print(i)
    return

#B
def B():
    return

#C
def C():
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#I
def I_():
    return

#J
def J():
    return

#Solve
if __name__ == "__main__":
    A()

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
e = [int(input()) for _ in range(m)]

used = [False] * (n + 1)

for temp in e[::-1]:
    if not used[temp]:
        used[temp] = True
        print(temp)

for i in range(1, n + 1):
    if not used[i]:
        print(i)

Submission status: Accepted

Solution code:
n,m = [int(i) for i in input().split()]
INIT = True
table = [INIT] * n
lis = []
for i in range(m):
  num = int(input())
  lis.append(num)

for i in lis[::-1]:
  if table[i-1]:
    print(i)
  table[i-1] = False

for i in range(n):
  if table[i]:
    print(i+1)