|
- #include<iostream>
- #include<algorithm>
- #include<cstring>
- using namespace std;
- const int mx=110;
- int n,k,a[mx];
- int dp[mx][mx],s[mx][mx];///dp[i][j]表示从前i只筷子中的最小值
- int main()///s[i][j]表示i和j的平方差
- {
- cin>>n>>k;
- for (int i=1; i<=n; i++) cin>>a[i];
- sort(a+1,a+n+1);///排序,使两只筷子之间的距离最小
- k=k+3;
- if (2*k>n) {cout<<"-1";return 0;}
- for (int i=1; i<=n; i++)
- for (int j=1; j<=n; j++)
- s[i][j]=(a[i]-a[j])*(a[i]-a[j]);
- memset(dp,127/3,sizeof(dp));///全部赋值为最大
- for (int i=0;i<=n;i++) dp[i][0]=0;
- for (int i=2; i<=n; i++)
- for (int j=1; j<=k; j++)
- dp[i][j]=min(dp[i-1][j],dp[i-2][j-1]+s[i-1][i]);
- cout<<dp[n][k];///用或不用第i只木棍
- return 0;
- }
复制代码 |
|