You are given an array a1,a2,…,an. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.
In other words, at most one time you can choose two integers l and r (1≤l≤r≤n) and delete integers al,al+1,…,ar from the array. Remaining elements should be pairwise distinct.
Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.
Input
The first line of the input contains a single integer n (1≤n≤2000) — the number of elements in the given array.
The next line contains n spaced integers a1,a2,…,an (1≤ai≤109) — the elements of the array.
Output
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.
Examples
inputCopy
3
1 2 3
outputCopy
0
inputCopy
4
1 1 2 2
outputCopy
2
inputCopy
5
1 4 1 4 9
outputCopy
2
Note
In the first example all the elements are already distinct, therefore no subsegment needs to be removed.
In the second example you can remove the subsegment from index 2 to 3.
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.
题意:给你n个数,然后让你从中删除一段子序列,使得删除后的数字中没有重复的数字,问你删除的子序列的最短长度
思路:思路的话就是通过暴力枚举子段,然后找出最短的长度,枚举的话就是先从最长的开始枚举,然后逐渐缩短,从两端枚举,逐渐缩小
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
const int inf=9999;
int v[2005];
set<int>s;
int main()
{
int n;
scanf("%d",&n);
for(int a=1;a<=n;a++)
{
scanf("%d",&v[a]);
s.insert(v[a]);
}
if(s.size()==n)
{
printf("0\n");
return 0;
}
int ans=inf;
for(int i=1;i<=n;i++) //从第一个数往后面缩小范围
{
s.clear();
for(int j=1;j<i;j++)
{
s.insert(v[j]);
}
for(int k=n;k>=i;k--)
{
int len=k-i+1;
if(s.size()==(n-len))
{
ans=min(ans,len);
}
s.insert(v[k]);
}
}
printf("%d\n",ans);
return 0;
}