PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000037. Truy vấn trên hoán vị với khóa (Queries on a Permutation With Key)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Truy vấn trên hoán vị với khóa (Queries on a Permutation With Key)

Queries on a Permutation With Key

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Initially, the permutation is P=[1,2,…,m]P=[1,2,\ldots,m]. Process qq query values in order. For each queried value xx, first record its current zero-based position in PP. Then remove xx from that position and place it at the front, preserving the relative order of all other elements.

Each query uses the permutation produced by all previous queries, and values may be queried repeatedly. Output the recorded positions in query order.

Input

  • Line 1: integers q,mq,m, the query count and permutation size.
  • Line 2: qq integers x1,x2,…,xqx_1,x_2,\ldots,x_q in query order.

Output

Print qq integers on one line: the zero-based position of each xix_i immediately before it is moved to the front.

Subtasks

Subtask 1 (20 points):

  • 1≤m≤201\le m\le 20; 1≤q≤m1\le q\le m; 1≤xi≤m1\le x_i\le m for all 1≤i≤q1\le i\le q.

Subtask 2 (30 points):

  • 1≤m≤2001\le m\le 200; 1≤q≤m1\le q\le m; 1≤xi≤m1\le x_i\le m for all 1≤i≤q1\le i\le q.

Subtask 3 (50 points):

  • 1≤m≤10001\le m\le 1000; 1≤q≤m1\le q\le m; 1≤xi≤m1\le x_i\le m for all 1≤i≤q1\le i\le q.

Examples

Examples 1

Input

4 5
3 1 2 1

Output

2 1 2 1

Explanation

Initially P=[1,2,3,4,5]P=[1,2,3,4,5]. Query 33 yields position 22 and changes PP to [3,1,2,4,5][3,1,2,4,5]. Query 11 yields 11 and changes PP to [1,3,2,4,5][1,3,2,4,5]. Query 22 yields 22 and changes PP to [2,1,3,4,5][2,1,3,4,5]. The last query 11 yields 11.

Examples 2

Input

4 4
4 1 2 2

Output

3 1 2 0

Explanation

The four recorded zero-based positions are 33, 11, 22, and finally 00, because the repeated value 22 is already at the front.

The permutation evolves as $[1,2,3,4] \to [4,1,2,3] \to [1,4,2,3] \to [2,1,4,3]$. The zero-based positions before these moves are 3,1,23,1,2. For the final query, 22 is already at the front, so its reported position is 00 and the permutation stays unchanged.