G - Minimum Permutation Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600600

問題文

11 以上 MM 以下の整数からなる長さ NN の数列 AA があります。ここで、11 以上 MM 以下のどの整数も AA11 回以上登場します。

AA の長さ MM の(連続とは限らない)部分列であって 1,,M1, \ldots, M11 回ずつ登場するもののうち、辞書順最小のものを答えてください。

制約

  • 1MN2×1051 \leq M \leq N \leq 2 \times 10^5
  • 1AiM1 \leq A_i \leq M
  • 11 以上 MM 以下のどの整数も AA11 回以上登場する。
  • 入力中の値はすべて整数である。

入力

入力は以下の形式で標準入力から与えられる。

NN MM
A1A_1 A2A_2 \ldots ANA_N

出力

求めるべき部分列を B1,,BMB_1, \ldots, B_M として、以下の形式で出力せよ。

B1B_1 B2B_2 \ldots BMB_M

入力例 1Copy

Copy
4 3
2 3 1 3

出力例 1Copy

Copy
2 1 3

AA の長さ 33 の部分列であって 1,2,31, 2, 311 回ずつ登場するものは (2,3,1)(2, 3, 1)(2,1,3)(2, 1, 3) であり、このうち辞書順で小さいのは (2,1,3)(2, 1, 3) です。


入力例 2Copy

Copy
4 4
2 3 1 4

出力例 2Copy

Copy
2 3 1 4

入力例 3Copy

Copy
20 10
6 3 8 5 8 10 9 3 6 1 8 3 3 7 4 7 2 7 8 5

出力例 3Copy

Copy
3 5 8 10 9 6 1 4 2 7

Score : 600600 points

Problem Statement

We have a sequence AA of length NN consisting of integers between 11 and MM. Here, every integer from 11 to MM appears at least once in AA.

Among the length-MM subsequences of AA where each of 1,,M1, \ldots, M appears once, find the lexicographically smallest one.

Constraints

  • 1MN2×1051 \leq M \leq N \leq 2 \times 10^5
  • 1AiM1 \leq A_i \leq M
  • Every integer between 11 and MM, inclusive, appears at least once in AA.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN MM
A1A_1 A2A_2 \ldots ANA_N

Output

Let B1,,BMB_1, \ldots, B_M be the sought subsequence, and print it in the following format:

B1B_1 B2B_2 \ldots BMB_M

Sample Input 1Copy

Copy
4 3
2 3 1 3

Sample Output 1Copy

Copy
2 1 3

The length-33 subsequences of AA where each of 1,2,31, 2, 3 appears once are (2,3,1)(2, 3, 1) and (2,1,3)(2, 1, 3). The lexicographically smaller among them is (2,1,3)(2, 1, 3).


Sample Input 2Copy

Copy
4 4
2 3 1 4

Sample Output 2Copy

Copy
2 3 1 4

Sample Input 3Copy

Copy
20 10
6 3 8 5 8 10 9 3 6 1 8 3 3 7 4 7 2 7 8 5

Sample Output 3Copy

Copy
3 5 8 10 9 6 1 4 2 7


2025-04-21 (Mon)
05:23:48 +00:00