The first thing you can see is a conclusion: it is definitely the number that is adjacent to each selection after the sort is finished.
DP[I][J] indicates that the minimum cost of the J Group is selected for the first I number.
#include <cstdio>#include<cstring>#include<cmath>#include<vector>#include<algorithm>using namespacestd;Const Long Longinf=999999999999999;Const intmaxn=2200;intn,k;Long LongA[MAXN],DP[MAXN][MAXN];intMain () { while(~SCANF ("%d%d",&n,&k)) { for(intI=1; i<=n; i++) scanf ("%lld",&A[i]); Sort (a+1, A +1+N); for(intI=0; i<=n;i++) for(intj=0; j<=k;j++) dp[i][j]=INF; for(intI=0; i<=n;i++) dp[i][0]=0; for(intI=1; i<=n;i++) { for(intj=1; j<=k;j++) {Dp[i][j]=dp[i-1][j]; if(dp[i-2][j-1]==inf)Continue; if(I-1==0)Continue; DP[I][J]=min (dp[i][j],dp[i-2][j-1]+abs (a[i]-a[i-1]) *abs (a[i]-a[i-1])); } } Long Longans=INF; for(intI=1; i<=n;i++) ans=min (ans,dp[i][k]); printf ("%lld\n", ans); } return 0;}
HDU 1421 Move Bedroom