PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#FT0000152. Khoảng cách trong hoán vị (Permutation Distance)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Khoảng cách trong hoán vị (Permutation Distance)
Permutation Distance
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a permutation . Associate index with the point . The distance between two distinct indices is (Manhattan distance). For every index , find its minimum distance to another point; choosing is forbidden. Output the answers in increasing order of indices.
Input
The first line contains ; the next line contains .
Output
Print integers in order, separated by spaces.
Subtasks
- Subtask 1 (20 points): ; The sequence is a permutation of : and all values are pairwise distinct.
- Subtask 2 (30 points): ; The sequence is a permutation of : and all values are pairwise distinct.
- Subtask 3 (50 points): ; The sequence is a permutation of : and all values are pairwise distinct.
Examples
Example 1
Input
4
3 2 4 1
Output
2 2 3 3
Explanation
For , distances to are , so . For , choose for distance . For , choose for distance . For , choose for distance .
Example 2
Input
7
1 2 3 4 5 6 7
Output
2 2 2 2 2 2 2
Explanation
Every adjacent pair has distance . No distinct pair can have distance below because both coordinate differences are positive integers.