标签:des blog http io ar os sp java for
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18194 Accepted Submission(s): 6171
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<string> #include<algorithm> #include<queue> #include<vector> using namespace std; int n,k; int a[2050],dp[2050][2050]; bool cmp(int a,int b) { return a<b; } int main() { while(scanf("%d%d",&n,&k)!=EOF) { memset(a,0,sizeof(a)); memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) scanf("%d",&a[i]); sort(a+1,a+1+n,cmp); dp[2][1]=(a[2]-a[1])*(a[2]-a[1]); for(int i=1;i<=k;i++) for(int j=i*2;j<=n;j++) { if(j>i*2) dp[j][i]=min(dp[j-1][i],dp[j-2][i-1]+(a[j]-a[j-1])*(a[j]-a[j-1])); else dp[j][i]=dp[j-2][i-1]+(a[j]-a[j-1])*(a[j]-a[j-1]); } printf("%d\n",dp[n][k]); } return 0; }
标签:des blog http io ar os sp java for
原文地址:http://www.cnblogs.com/a972290869/p/4099654.html