标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1421
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23904 Accepted Submission(s): 8177
if(j*2<=i-1) dp[i][j]=min(dp[i-1][j],dp[i-2][j-1]+(weight[i]-weight[i-1])*(weight[i]-weight[i-1])); else dp[i][j]=dp[i-2][j-1]+(weight[i]-weight[i-1])*(weight[i]-weight[i-1]); //不可能从前i-1个中拿出j对
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; //#define INF 9223372036854775807 #define LL long long LL weight[2005]; LL dp[2005][1002]; ///前i个中找j对的最小值 int main() { int n,k; while(scanf("%d%d",&n,&k)!=EOF) { for(int i=1; i<=n; i++) scanf("%I64d",&weight[i]); sort(weight+1,weight+n+1); for(int i=2; i<=n; i++) for(int j=1; 2*j<=i; j++) { if(j*2<=i-1) dp[i][j]=min(dp[i-1][j],dp[i-2][j-1]+(weight[i]-weight[i-1])*(weight[i]-weight[i-1])); else dp[i][j]=dp[i-2][j-1]+(weight[i]-weight[i-1])*(weight[i]-weight[i-1]); } printf("%I64d\n",dp[n][k]); } return 0; }
标签:
原文地址:http://www.cnblogs.com/jasonlixuetao/p/5425180.html