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;
}