http://acm.hdu.edu.cn/showproblem.php?pid=2546
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=3361
http://blog.sina.com.cn/s/blog_5123df350100e8p8.html
http://blog.sina.com.cn/s/blog_51cea4040100gvn3.html
http://blog.csdn.net/wukonwukon/article/details/6939050
先对a[]按小到大排序.设:dp[i][j]表示前 i 个物品中搬 j 对的最少疲劳度.
显然,当i==2*j时, dp[i][j]=dp[i-2][j-1] + (a[i]-a[i-1])^2
因为,当 (a1-a2)^2+(a3-a4)^2 <= (a1-a4)^2+(a3-a2)^2 ( a1<=a2<=a3<=a4 ).
当i>2*j时, dp[i][j] = min( dp[i-1][j] , dp[i-2][j-1]+(a[i]-a[i-1])^2 )
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 int dp[2100][1100]; 5 int a[2100]; 6 int n,k; 7 int pow2(int a) {return a*a;} 8 int min2(int a,int b) {return a<b?a:b;} 9 10 int cmp(const void* a,const void* b){ 11 return *(int*)a-*(int*)b; 12 } 13 14 int main(){ 15 int i,j,l; 16 while(EOF != scanf("%d%d",&n,&k)){ 17 for(i=1;i<=n;i++) 18 scanf("%d",&a[i]); 19 qsort(a+1,n,sizeof(a[0]),cmp); 20 memset(dp,0,sizeof(dp)); 21 for(i=2;i<=n;i++){ 22 l=i/2; 23 for(j=1;j<=l;j++){ 24 if(i==2*j) 25 dp[i][j]=dp[i-2][j-1]+pow2(a[i]-a[i-1]); 26 else 27 dp[i][j]=min2(dp[i-1][j],dp[i-2][j-1]+pow2(a[i]-a[i-1])); 28 } 29 } 30 printf("%d\n",dp[n][k]); 31 } 32 return 0; 33 }
1 #include <stdio.h> 2 int f[5843],n; 3 int i,j,k,l; 4 5 int min(int a,int b,int c,int d){ 6 int min=a; 7 if(b<min) min=b; 8 if(c<min) min=c; 9 if(d<min) min=d; 10 11 if(a==min) i++; 12 if(b==min) j++; 13 if(c==min) k++; 14 if(d==min) l++; 15 16 return min; 17 } 18 19 int main(){ 20 i=j=k=l=1; 21 f[1]=1; 22 for(int t=2;t<=5842;t++) 23 f[t]=min(2*f[i],3*f[j],5*f[k],7*f[l]); 24 25 while(scanf("%d",&n)&&n!=0){ 26 if(n%10==1&&n%100!=11) 27 printf("The %dst humble number is %d.\n",n,f[n]); 28 else if(n%10==2&&n%100!=12) 29 printf("The %dnd humble number is %d.\n",n,f[n]); 30 else if(n%10==3&&n%100!=13) 31 printf("The %drd humble number is %d.\n",n,f[n]); 32 else 33 printf("The %dth humble number is %d.\n",n,f[n]); 34 } 35 return 0; 36 }