#FT0000037. Truy vấn trên hoán vị với khóa (Queries on a Permutation With Key)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 . Process query values in order. For each queried value , first record its current zero-based position in . Then remove 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 , the query count and permutation size.
- Line 2: integers in query order.
Output
Print integers on one line: the zero-based position of each immediately before it is moved to the front.
Subtasks
Subtask 1 (20 points):
- ; ; for all .
Subtask 2 (30 points):
- ; ; for all .
Subtask 3 (50 points):
- ; ; for all .
Examples
Examples 1
Input
4 5
3 1 2 1
Output
2 1 2 1
Explanation
Initially . Query yields position and changes to . Query yields and changes to . Query yields and changes to . The last query yields .
Examples 2
Input
4 4
4 1 2 2
Output
3 1 2 0
Explanation
The four recorded zero-based positions are , , , and finally , because the repeated value 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 . For the final query, is already at the front, so its reported position is and the permutation stays unchanged.