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 MBSubmit: 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 101
9
11
19
Sample Output
44dp[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;
}
相关文章推荐
- [bzoj1742][Usaco2005 nov][DP]Grazing on the Run 边跑边吃草
- 【bzoj1742】[Usaco2005 nov]Grazing on the Run 边跑边吃草 区间dp
- bzoj 1742: [Usaco2005 nov]Grazing on the Run 边跑边吃草【区间dp】
- BZOJ 1742: [Usaco2005 nov]Grazing on the Run 边跑边吃草( dp )
- 【BZOJ1742】[Usaco2005 nov]Grazing on the Run 边跑边吃草【区间DP】
- BZOJ 1742 Usaco2005 nov Grazing on the Run 边跑边吃草 动态规划
- 【bzoj1742】[Usaco2005 nov]Grazing on the Run 边跑边吃草
- BZOJ1742: [Usaco2005 nov]Grazing on the Run 边跑边吃草
- [Usaco2005 nov]Grazing on the Run 边跑边吃草 BZOJ1742
- BZOJ 1694 [Usaco2007 Demo]Grazing on the Run DP
- BZOJ 1694 & 1742 [Usaco 2005 nov] 区间DP 解题报告
- [Usaco2005 Nov Gold]Grazing on the Run奶牛吃草-Dp 移动类
- 1694: [Usaco2007 Demo]Grazing on the Run
- BZOJ 3384: [Usaco2004 Nov]Apple Catching 接苹果/BZOJ 1750: [Usaco2005 qua]Apple Catching dp
- bzoj 1630 && 2023: [Usaco2005 Nov]Ant Counting 数蚂蚁(有重复元素的组合数)
- BZOJ 1605 [Usaco2008 Open]Crisis on the Farm 牧场危机 DP
- 【BZOJ1589】[Usaco2008 Dec]Trick or Treat on the Farm 采集糖果【SCC】【基环外向树】【DP】【记忆化搜索】
- BZOJ 2023: [Usaco2005 Nov]Ant Counting 数蚂蚁——DP
- bzoj1753 [Usaco2005 qua]Who's in the Middle
- POJ 3042 Grazing on the Run (三维区间DP)【区间DP模板】