Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 999999/400000 K (Java/Others)
Total Submission(s): 5053 Accepted Submission(s): 1980
1 #pragma comment(linker, "/STACK:102400000,102400000") 2 #include <cstdio> 3 #include <iostream> 4 #include <cstdlib> 5 #include <cstring> 6 #include <algorithm> 7 #include <cmath> 8 #include <cctype> 9 #include <map> 10 #include <set> 11 #include <queue> 12 #include <bitset> 13 #include <string> 14 #include <complex> 15 #define ll __int64 16 #define mod 1000000007 17 using namespace std; 18 int t; 19 int n,m; 20 int a[10004]; 21 int dp[5005][10004]; 22 int q[10004],head,tail; 23 int main() 24 { 25 scanf("%d",&t); 26 for(int s=1; s<=t; s++) 27 { 28 scanf("%d %d",&n,&m); 29 for(int j=1; j<=n; j++) 30 scanf("%d",&a[j]); 31 sort(a+1,a+1+n); 32 for(int j=1; j<=n; j++) 33 dp[1][j]=(a[j]-a[1])*(a[j]-a[1]); 34 for(int i=2; i<=m; i++) 35 { 36 head=tail=0; 37 q[tail++]=i-1; 38 for(int j=i; j<=n; j++) 39 { 40 while(head+1<tail) 41 { 42 int p1=q[head],p2=q[head+1]; 43 int x1=a[p1+1],x2=a[p2+1]; 44 int y1=dp[i-1][p1]+x1*x1,y2=dp[i-1][p2]+x2*x2; 45 if(y2-y1<2*a[j]*(x2-x1)) 46 head++; 47 else 48 break; 49 } 50 int k=q[head]; 51 dp[i][j]=dp[i-1][k]+(a[j]-a[k+1])*(a[j]-a[k+1]); 52 while(head+1<tail&&j!=n) 53 { 54 int p1=q[tail-2],p2=q[tail-1],p3=j; 55 int x1=a[p1+1],x2=a[p2+1],x3=a[p3+1]; 56 int y1=dp[i-1][p1]+x1*x1,y2=dp[i-1][p2]+x2*x2,y3=dp[i-1][p3]+x3*x3; 57 if((y3-y2)*(x2-x1)<=(y2-y1)*(x3-x2)) 58 tail--; 59 else 60 break; 61 } 62 q[tail++]=j; 63 } 64 } 65 printf("Case %d: %d\n",s,dp[m][n]); 66 } 67 return 0; 68 }
原文:http://www.cnblogs.com/hsd-/p/7257643.html