#P15259. [USACO26JAN2] Farmer John Loves Rotations S

    ID: 15163 远端评测题 2000ms 256MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>贪心线段树USACOST 表2026双指针 two-pointer

[USACO26JAN2] Farmer John Loves Rotations S

Description

Farmer John has an array AA containing NN integers (1N5105,1AiN1 \leq N \leq 5 \cdot 10^5, 1 \leq A_i \leq N). He picks his favorite index jj and take out a sheet of paper with only AjA_j written on it. He can then perform the following operation some number of times:

  • Cyclically shift all elements in AA one spot to the left or one spot to the right. Then, write down AjA_j on a piece of paper.

Let SS denote the set of distinct integers that occur in AA. Farmer John wonders what the minimum number of operations he must perform is so that the paper contains all integers that appear in SS.

Since it is unclear what FJ's favorite index is, output the answer for all possible favorite indices 1jN1 \leq j \leq N. Note for each index, AA is reset to its original form before performing any operations.

Input Format

The first line contains NN.

The following line contains A1,A2,,ANA_1, A_2, \ldots, A_N.

Output Format

Output NN space-separated integers, where the ii'th integer is the answer for his favorite index j=ij = i.

6
1 2 3 1 3 4
4 3 3 4 3 3
12
1 1 2 1 1 3 1 1 4 1 1 1
8 7 6 7 8 9 8 7 6 7 8 9

Hint

Sample 1 Explanation

The distinct numbers are S={1,2,3,4}S = \{ 1, 2, 3, 4 \}. Suppose Farmer John’s favorite index is j=1j=1. He starts off with A1=1A_1=1 written on a piece of paper. We can track the array AA after each cyclic shift Farmer John makes.

  • Cyclic shift right: FJ writes down A1=4A_1 = 4. :::align{center} 4 1 2 3 1 3 :::
  • Cyclic shift left: FJ writes down A1=1A_1 = 1 again. :::align{center} 1 2 3 1 3 4 :::
  • Cyclic shift left: FJ writes down A1=2A_1 = 2. :::align{center} 2 3 1 3 4 1 :::
  • Cyclic shift left: FJ writes down A1=3A_1 = 3. :::align{center} 3 1 3 4 1 2 ::: At this point, Farmer John has written down every number in SS using 4 operations.

SCORING:

  • Inputs 3-5: N500N\le 500
  • Inputs 6-8: N104N\le 10^4
  • Inputs 9-17: No additional constraints.