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

B. Uniqueness

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array a1,a2,…,ana1,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 ll and rr (1≤l≤r≤n1≤l≤r≤n) and delete integers al,al+1,…,aral,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 nn (1≤n≤20001≤n≤2000) — the number of elements in the given array.

The next line contains nn spaced integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤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 00.

Examples

input

Copy

3
1 2 3

output

Copy

0

input

Copy

4
1 1 2 2

output

Copy

2

input

Copy

5
1 4 1 4 9

output

Copy

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 22 to 33.

In the third example you can remove the subsegments from index 11 to 22, or from index 22 to 33, or from index 33 to 44.

 

题意:给你n个数,让你从这段数中删除最短的一段连续的数,使得剩下的数字不会重复。

题解:对这n个数离散化之后,模拟即可。

#include <bits/stdc++.h>

using namespace std;

int main(){
    int n, total = 0, res = 0;
    scanf("%d", &n);
    vector <int> a(n);
    for (int i = 0; i < n; i++){
        scanf("%d", &a[i]);
    }
    vector <int> b = a;
    sort(b.begin(), b.end());
    b.resize(unique(b.begin(), b.end()) - b.begin());
    if ((int)b.size() == n){
        cout << "0" << "\n";
        return 0;
    }
    for (int i = 0; i < n; i++){//离散化
        a[i] = (int)(lower_bound(b.begin(), b.end(), a[i]) - b.begin());
    }
    vector<int> mark(n, 0);
    for (int i = 0; i < n; i++){
        total = 0;
        for (int j = 0; j < n; j++){
            if(mark[a[n - j - 1]] > 0){
                break;
            }
            mark[a[n - j - 1]] = 1;
            total++;
        }
        res = max(res, i + total);
        for (int j = 0; j < total; j++){
            mark[a[n - j - 1]] = 0;
        }
        if(mark[a[i]] > 0){
            break;
        }
        mark[a[i]] = 1;
    }
    cout << n - res << "\n";
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值