Gym 100090M Jumping along the Hummocks

时间:2023-03-09 03:53:09
Gym 100090M Jumping along the Hummocks

Gym 100090M Jumping along the Hummocks

题意:

从 前往后跳,要么跳一步,跳到相邻的位置,要么跳到下一个数字相同的位置,求跳到最后的最少步数。

dp,但是会tle,我用map优化了一下。

 #include <bits/stdc++.h>

 using namespace std;

 const int inf = 0x3f3f3f3f;
int a[];
int dp[]; int main() { int n;
while(~scanf("%d",&n)) {
for(int i=;i<n;i++) {
scanf("%d",&a[i]);
} memset(dp,inf,sizeof(dp));
dp[] = ;
map<int,int> maps;
maps[a[]] = ;
for(int i=;i<n;i++) {
dp[i] = dp[i-] + ;
if(maps.count(a[i])>) {
dp[i] = min(dp[i],maps[a[i]] + );
maps[a[i]] = dp[i];
}
else {
maps[a[i]] = dp[i];
}
}
printf("%d\n",dp[n-]); } return ;
}