因为k很小,所以无需优化dp
#include<bits/stdc++.h>
using namespace std;
void solve(){
int n,k;
cin>>n>>k;
vector<int>a(n+1);
vector<int>dp(n+1,1e18);
dp[1]=0;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=2;i<=n;i++){
for(int j=1;j<=k&&i-j>=1;j++){
dp[i]=min(dp[i],dp[i-j]+abs(a[i]-a[i-j]));
}
}
cout<<dp[n];
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t=1;
//cin>>t;
for(int i=1;i<=t;i++)solve();
return 0;
}
标签:cout,int,namespace,Frog,abs,dp
From: https://www.cnblogs.com/yufan1102/p/17924278.html