Uniqueness(思维+模拟)

https://codeforces.com/contest/1208/problem/B

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 ll and rr (1≤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≤2000) — the number of elements in the given array.

The next line contains nn spaced integers a1,a2,…,ana1,a2,…,an (1≤ai≤10^9) — 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

input

3
1 2 3

output

0

input

4
1 1 2 2

output

2

input

5
1 4 1 4 9

output

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.

题意:在原序列上删连续的一段,使剩下的数中没有重复出现的数

思路:dp[i]表示左边留到下标i所能剩下的不重复的数的最大数目

 ans=n-max(dp[i])

太菜了,能暴力解决的问题为啥非要加上自己的想当然的剪枝呢,连续两场被rejudge了B,卑微

#include<bits/stdc++.h>
#include<tr1/unordered_map>
#pragma GCC optimize(3)
#define max(a,b) a>b?a:b
using namespace std;
typedef long long ll;
int a[2005];
unordered_map<int,int> mpl,mpr;
vector<int> v;
int dp[2005];
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=n;i>=1;i--){
    	if(mpr[a[i]]==0){
    		mpr[a[i]]=1;
    		v.push_back(a[i]);
		}
		else break;
	}
    dp[0]=v.size();
    int pos;
    for(pos=1;pos<=n;pos++){
    	if(mpl[a[pos]]==0){
    		mpl[a[pos]]=1;
    		if(mpr[a[pos]]==1){
    			while(v.back()!=a[pos]){
    				mpr[v.back()]=0;
    				v.pop_back();
				}
				mpr[v.back()]=0;
    			v.pop_back();
			}
			dp[pos]=pos+v.size();
		}
		else break;
	}
	int ans=0;
	for(int i=0;i<=pos;i++) ans=max(ans,dp[i]);
	printf("%d\n",n-ans);
	return 0;
}

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值