#BS0000167. Xóa một đoạn để các phần tử còn lại phân biệt (Uniqueness)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Xóa một đoạn để các phần tử còn lại phân biệt (Uniqueness)
Uniqueness
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an array . You may remove at most one contiguous subarray: choose , delete positions through , and concatenate the remaining prefix and suffix without changing their relative order.
Afterwards, all remaining values must be pairwise distinct: no two remaining positions may have the same value.
Find the minimum number of removed elements. If the original array already has distinct elements, you may remove nothing and print 0.
Input
- The first line contains an integer , the array length.
- The second line contains exactly integers .
Output
Print one integer: the minimum length of a removed contiguous subarray that makes all remaining elements distinct; print 0 if no removal is necessary.
Subtasks
- Subtask 1 (20 points): ; for every .
- Subtask 2 (30 points): ; for every .
- Subtask 3 (50 points): ; for every .
Examples
Input
4
1 1 2 2
Output
2
Explanation
The initial array is . Remove positions and , namely the middle subarray . The remaining array is , which has distinct values.
Removing just one element is insufficient: initially there are two duplicated values (two copies of and two copies of ), and removing one element can eliminate at most one duplicate. Thus at least elements must be removed, and the output is 2.