Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) B. Uniqueness

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值