标签:des style blog http color java os io
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17065 Accepted Submission(s): 5785
n个物品选K对
可以推出状态方程为:
dp[i][k] = min(dp[i-1][k],dp[i-2][k-1]+(a[i]-a[i-1])*(a[i]-a[i-1]))
dp[i][k] 表示k对物品在前i个物品中的最小值。
1 #include<cstdio> 2 #include<cstring> 3 #include<stdlib.h> 4 #include<algorithm> 5 using namespace std; 6 int a[2000]; 7 int dp[2000][1000]; 8 int main() 9 { 10 int n,k; 11 while(scanf("%d %d",&n,&k)!=EOF) 12 { 13 memset(a,0,sizeof(a)); 14 memset(dp,0,sizeof(dp)); 15 for(int i=1;i<=n;i++) 16 scanf("%d",&a[i]); 17 sort(a+1,a+n+1); 18 for(int i=1;i<=k;i++) 19 { 20 for(int j=2*i;j<=n;j++) 21 { 22 if(j==2*i) 23 dp[j][i]=dp[j-2][i-1]+(a[j]-a[j-1])*(a[j]-a[j-1]); 24 else 25 dp[j][i]=min(dp[j-1][i],dp[j-2][i-1]+(a[j]-a[j-1])*(a[j]-a[j-1])); 26 } 27 } 28 printf("%d\n",dp[n][k]); 29 } 30 return 0; 31 }
HDU 1421 搬寝室 (DP),布布扣,bubuko.com
标签:des style blog http color java os io
原文地址:http://www.cnblogs.com/clliff/p/3898488.html