题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1227
状态很好定义,关键是怎么递推,加入一个仓库后的影响怎么算出来
思路:http://www.cnblogs.com/jackge/archive/2013/03/27/2984463.html
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=200+5;
const int INF=0x3f3f3f3f;
int rest[maxn],d[maxn][maxn],cost[maxn][maxn];
int inline getCost(int x,int y)
{if(cost[x][y]) return cost[x][y];int mid=(x+y)/2;int sum=0;for(int i=x;i<=y;i++)sum+=abs(rest[i]-rest[mid]);return cost[x][y]=sum;
}
int main(int argc, char const *argv[])
{int n,k,kase=0; while(cin>>n>>k,(n+k)){memset(d,INF,sizeof(d));memset(cost,0,sizeof(cost));for(int i=1;i<=n;i++)scanf("%d",&rest[i]);for(int i=1;i<=k;i++)for(int j=1;j<=n;j++){if(i==1) d[1][j]=getCost(1,j);else for(int m=i-1;m<=j-1;m++)d[i][j]=min(d[i-1][m]+getCost(m+1,j),d[i][j]);}printf("Chain %d\nTotal distance sum = %d\n\n",++kase,d[k][n]);}return 0;
}