hdu1421 搬寢室(dp)
/*
先對n中物品的重量排序
令dp[i][j]表示前i個物品中選j對的最小疲勞度。
則dp[i][j]可能含有第i個物品(這種情況下,第i種物品一定是和第i-1個物品配對),
則dp[i][j]=dp[i-2][j-1]+(val[i]-val[i-1])*(val[i]-val[i-1])
dp[i][j]的j對也可能不含有第i個物品,此時有
dp[i][j]=dp[i-1][j]
狀態轉移方程
dp[i][j]=min{dp[i-2][j-1]+(val[i]-val[i-1])*(val[i]-val[i-1]),dp[i-1][j]
*/
# include
# include
# include
# define INF 999999999
using namespace std;
int dp[2100][2100];
int a[2100];
int main()
{
int n,k,i,j;
while(~scanf("%d%d",&n,&k))
{
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1);
for(i=0;i<=n;i++)
for(j=1;j<=k;j++)
dp[i][j]=INF;
for(i=2;i<=n;i++)
{
for(j=1;j*2<=i;j++)
{
dp[i][j]=min(dp[i-1][j],dp[i-2][j-1]+(a[i]-a[i-1])*(a[i]-a[i-1]));
}
}
printf("%d\n",dp[n][k]);
}
return 0;
}