首页 > 代码库 > BZOJ1742[Usaco2005 nov]Grazing on the Run 边跑边吃草

BZOJ1742[Usaco2005 nov]Grazing on the Run 边跑边吃草

费用提前计算的DP..

技术分享
 1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6 #define INF 0x3f3f3f3f 7 #define MAXN 1005 8 int dp[2][MAXN][MAXN]; 9 int x[MAXN];10 int n,x0;11 int main()12 {13     scanf("%d%d",&n,&x0);14     for(int i=1;i<=n;i++)15         scanf("%d",&x[i]);16     x[++n]=x0;17     sort(x+1,x+n+1);18     for(int i=1;i<=n;i++)19         if(x[i]!=x0)dp[0][i][i]=dp[1][i][i]=INF;20         else21             dp[0][i][i]=dp[1][i][i]=0;22     for(int i=n;i>=1;i--)23         for(int j=i+1;j<=n;j++)24         {25             dp[0][i][j]=min(dp[0][i+1][j]+(x[i+1]-x[i])*(n-j+i),26                             dp[1][i+1][j]+(x[j]-x[i])*(n-j+i));27             dp[1][i][j]=min(dp[1][i][j-1]+(x[j]-x[j-1])*(n-j+i),28                             dp[0][i][j-1]+(x[j]-x[i])*(n-j+i));29         }30         printf("%d\n",min(dp[0][1][n],dp[1][1][n]));31         return 0;                                                      32 }
View Code

 

BZOJ1742[Usaco2005 nov]Grazing on the Run 边跑边吃草