/* 先对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 <algorithm> # include <stdio.h> # include <string.h> # 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; }
时间: 2024-10-22 00:09:17