1208B.md (1580B)
1 Source: https://codeforces.com/problemset/problem/1208/B 2 3 B. Uniqueness 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. 22 23 In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. 24 25 Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. 26 27 Input 28 29 The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. 30 31 The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. 32 33 Output 34 35 Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. 36 37 Examples 38 39 Input 40 41 ``` 42 3 43 1 2 3 44 ``` 45 46 Output 47 48 ``` 49 0 50 ``` 51 52 Input 53 54 ``` 55 4 56 1 1 2 2 57 ``` 58 59 Output 60 61 ``` 62 2 63 ``` 64 65 Input 66 67 ``` 68 5 69 1 4 1 4 9 70 ``` 71 72 Output 73 74 ``` 75 2 76 ``` 77 78 Note 79 80 In the first example all the elements are already distinct, therefore no subsegment needs to be removed. 81 82 In the second example you can remove the subsegment from index $2$ to $3$. 83 84 In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.