您的位置:首页 > 其它

bzoj 1694 && 1742: [Usaco2005 nov]Grazing on the Run 边跑边吃草(DP)

2017-09-19 21:14 761 查看

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

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 395  Solved: 215

[Submit][Status][Discuss]

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

Sample Output

44

dp[l][r][1]表示当前吃完了[l, r]的草,牛在r位置的最小值

dp[l][r][0]表示当前吃完了[l, r]的草,牛在l位置的最小值

初始化:dp[l][l][1] = dp[l][l][0] = abs(L-a[l])*n(L为起始位置)

转移:

dp[l][r][t] = min(dp[l][r-1][1]+(a[r]-a[r-1])*(n-(r-l)), dp[l][r-1][0]+(a[r]-a[l])*(n-(r-l)));

dp[l][r][t] = min(dp[l+1][r][0]+(a[l+1]-a[l])*(n-(r-l)), dp[l+1][r][1]+(a[r]-a[l])*(n-(r-l)));

#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
using namespace std;
int n, p, a[1005], dp[1005][1005][2];
int Sech(int l, int r, int t)
{
if(dp[l][r][t]!=-1)
return dp[l][r][t];
if(l==r)
dp[l][r][t] = abs(p-a[r])*n;
else
{
if(t==1)
dp[l][r][t] = min(Sech(l, r-1, 1)+(a[r]-a[r-1])*(n-(r-l)), Sech(l, r-1, 0)+(a[r]-a[l])*(n-(r-l)));
else
dp[l][r][t] = min(Sech(l+1, r, 0)+(a[l+1]-a[l])*(n-(r-l)), Sech(l+1, r, 1)+(a[r]-a[l])*(n-(r-l)));
}
return dp[l][r][t];
}
int main(void)
{
int i;
scanf("%d%d", &n, &p);
memset(dp, -1, sizeof(dp));
for(i=1;i<=n;i++)
scanf("%d", &a[i]);
sort(a+1, a+n+1);
printf("%d\n", min(Sech(1, n, 0), Sech(1, n, 1)));
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: