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

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

1 lộ trình

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 P=(P1,…,Pn)P=(P_1,\ldots,P_n). Associate index ii with the point (i,Pi)(i,P_i). The distance between two distinct indices i,ji,j is ∣i−j∣+∣Pi−Pj∣|i-j|+|P_i-P_j| (Manhattan distance). For every index ii, find its minimum distance to another point; choosing j=ij=i is forbidden. Output the answers in increasing order of indices.

Input

The first line contains nn; the next line contains P1,…,PnP_1,\ldots,P_n.

Output

Print nn integers D1,…,DnD_1,\ldots,D_n in order, separated by spaces.

Subtasks

  • Subtask 1 (20 points): 2≤n≤122\le n\le 12; The sequence PP is a permutation of 1,2,…,n1,2,\ldots,n: 1≤Pi≤n1\le P_i\le n and all values are pairwise distinct.
  • Subtask 2 (30 points): 2≤n≤30002\le n\le 3000; The sequence PP is a permutation of 1,2,…,n1,2,\ldots,n: 1≤Pi≤n1\le P_i\le n and all values are pairwise distinct.
  • Subtask 3 (50 points): 2≤n≤2000002\le n\le 200000; The sequence PP is a permutation of 1,2,…,n1,2,\ldots,n: 1≤Pi≤n1\le P_i\le n and all values are pairwise distinct.

Examples

Example 1

Input

4
3 2 4 1

Output

2 2 3 3

Explanation

For i=1i=1, distances to j=2,3,4j=2,3,4 are 2,3,52,3,5, so D1=2D_1=2. For i=2i=2, choose j=1j=1 for distance 22. For i=3i=3, choose j=1j=1 for distance 33. For i=4i=4, choose j=2j=2 for distance ∣4−2∣+∣1−2∣=3|4-2|+|1-2|=3.

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 1+1=21+1=2. No distinct pair can have distance below 22 because both coordinate differences are positive integers.