标签:des style blog io color os sp java for
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18191 Accepted Submission(s): 6170
1 /* 2 dp(i,j)表示:i个数内,选出j对数的最优策略 3 dp(i,j)=max(dp(i-1,j),dp(i-2,j-1)+(a[i]-a[i-1])^2) 4 */ 5 #include <iostream> 6 #include <cstdio> 7 #include <cstring> 8 using namespace std; 9 10 typedef __int64 LL; 11 const LL INF=1e15; 12 const int maxn=2005; 13 int a[maxn]; 14 LL dp[maxn][maxn]; 15 inline LL min(LL a,LL b){return a<b?a:b;} 16 17 void quicksort(int l,int r) 18 { 19 if(l<r) 20 { 21 int t=a[l],i=l,j=r; 22 while(i!=j) 23 { 24 while(a[j]>=t && i<j) j--; 25 while(a[i]<=t && i<j) i++; 26 if(i<j) swap(a[i],a[j]); 27 } 28 a[l]=a[i];a[i]=t; 29 quicksort(l,i-1); 30 quicksort(i+1,r); 31 } 32 } 33 34 int main() 35 { 36 int n,k,i,j; 37 while(~scanf("%d%d",&n,&k)) 38 { 39 for(i=1;i<=n;i++) scanf("%d",a+i); 40 quicksort(1,n); 41 for(i=0;i<=n;i++) 42 for(j=0;j<=k;j++) 43 dp[i][j]=INF; 44 for(i=0;i<=n;i++) dp[i][0]=0; 45 for(i=2;i<=n;i++) 46 { 47 for(j=1;j<=k && j*2<=i;j++) 48 dp[i][j]=min(dp[i-1][j],dp[i-2][j-1]+(a[i]-a[i-1])*(a[i]-a[i-1])); 49 } 50 printf("%I64d\n",dp[n][k]); 51 } 52 return 0; 53 }
标签:des style blog io color os sp java for
原文地址:http://www.cnblogs.com/xiong-/p/4098634.html