文章
12
粉丝
0
获赞
12
访问
629
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int dp[N];//到第i个路牌的最短时间
int a[N];
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
memset(dp,0x3f,sizeof(dp));
dp[1] = 0;
map<int,int> mp;//mp[i] = j:路牌id为i,他上一次出现的地方
int n;
cin>>n;
for(int i = 1 ; i <= n; i++){
cin>>a[i];
}
mp[a[1]] = 1;
for(int i = 2; i <=n; i++){
dp[i] = min(dp[i],dp[i-1]+1);
if(mp.count(a[i])){
int pre_pos = mp[a[i]];
dp[i] = min(dp[i], dp[pre_pos] + 1);
}
mp[a[i]] = i;
}
cout<<dp[n]<<endl;
}
登录后发布评论
暂无评论,来抢沙发