标签:des style blog http io color os ar java
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17885 Accepted Submission(s): 6065
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; #define min(a,b) ((a)<(b)?(a):(b)) #define INF 0x7ffffff #define N 2002 int a[N]; int dp[N][N]; //dp[i][j]表示前i个物品中搬j个的最小值 int main() { int n,k,i,j; while(scanf("%d%d",&n,&k)!=EOF) { for(i=1;i<=n;i++) { scanf("%d",&a[i]); } sort(a+1,a+n+1); //由于要差的平方最小,所以排序,每次考虑相邻的两个 for(i=1;i<=n;i++) { dp[i][0]=0; //初始化前i个物品搬0个最小值为0,其他为最大 for(j=1;j<=n;j++) { dp[i][j]=INF; } } for(i=1;i<=n;i++) { for(j=0;j<=i;j+=2) { dp[i][j]=min(dp[i-1][j],dp[i-2][j-2]+(a[i]-a[i-1])*(a[i]-a[i-1])); } } cout<<dp[n][2*k]<<endl; } return 0; }
标签:des style blog http io color os ar java
原文地址:http://www.cnblogs.com/hate13/p/4054538.html