【bzoj1742】[Usaco2005 nov]Grazing on the Run 边跑边吃草

Description

John养了一只叫Joseph的奶牛。一次她去放牛,来到一个非常长的一片地,上面有N块地方长了茂盛的草。我们可
以认为草地是一个数轴上的一些点。Joseph看到这些草非常兴奋,它想把它们全部吃光。于是它开始左右行走,吃
草。John和Joseph开始的时候站在p位置。Joseph的移动速度是一个单位时间一个单位距离。不幸的是,草如果长
时间不吃,就会腐败。我们定义一堆草的腐败值是从Joseph开始吃草到吃到这堆草的总时间。Joseph可不想吃太腐
败的草,它请John帮它安排一个路线,使得它吃完所有的草后,总腐败值最小。John的数学很烂,她不知道该怎样
做,你能帮她么?
Input

  • Line 1 : Two space-separated integers: N and L. N<=1000
  • Lines 2..N+1: Each line contains a single integer giving the position P of a clump (1 <= P <= 1,000,000).
    Output

  • Line 1: A single integer: the minimum total staleness Bessie can achieve while eating all the clumps.

Sample Input

4 10

1

9

11

19

INPUT DETAILS:

Four clumps: at 1, 9, 11, and 19. Bessie starts at location 10.
Sample Output

44

OUTPUT DETAILS:

Bessie can follow this route:

  • start at position 10 at time 0

  • move to position 9, arriving at time 1

  • move to position 11, arriving at time 3

  • move to position 19, arriving at time 11

  • move to position 1, arriving at time 29

giving her a total staleness of 1+3+11+29 = 44. There are other routes

with the same total staleness, but no route with a smaller one.44

题解
dp[i][j][0/1]表示从第i快草开始,吃了连续j快草,现在在左端或是右端的最小腐败程度。

代码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#define mod 10007
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,p,a[3030];
long long f[3030][2][2];
int main()
{
    n=read();p=read();
    for (int i=1;i<=n;i++) a[i]=read();
    a[++n]=p;sort(a+1,a+n+1);
    p=lower_bound(a+1,a+n+1,p)-a;
    memset(f,0x3f,sizeof(f));
    f[p][1][0]=f[p][1][1]=0;
    for (int j=2;j<=n;j++)
    {
        for (int i=1;i<=n;i++) f[i][j&1][0]=f[i][j&1][1]=0x3f3f3f3f;
        for (int i=max(p-j+1,1);i<=min(p,n-j+1);i++)
        {
            if (i!=p) f[i][j&1][0]=min(f[i+1][~j&1][0]+abs(a[i+1]-a[i])*(n-j+1),f[i+1][~j&1][1]+abs(a[i+j-1]-a[i])*(n-j+1));
            if (i!=p-j+1) f[i][j&1][1]=min(f[i][~j&1][1]+abs(a[i+j-1]-a[i+j-2])*(n-j+1),f[i][~j&1][0]+abs(a[i+j-1]-a[i])*(n-j+1));
        }
    }
    printf("%lld",min(f[1][n&1][0],f[1][n&1][1]));
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值