dp...

dp( l , r , k )  , 表示 吃了[ l , r ] 的草 , k = 1 表示最后在 r 处 , k = 0 表示最后在 l 处 .

--------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
 
#define rep( i , n ) for( int i = 0 ; i < n ; ++i )
#define clr( x , c ) memset( x , c , sizeof( x ) )
 
using namespace std;
 
const int maxn = 1000 + 5;
const int inf = 0x3f3f3f3f;
 
int d[ maxn * maxn >> 1 ][ 2 ];
int h[ maxn ];
int n;
 
#define f( l , r ) ( ( ( ( ( n << 1 ) - l ) * l ) >> 1 ) + r )
 
// k 0 L , k 1 R
int dp( int l , int r , int k ) {
int &ans = d[ f( l , r ) ][ k ];
if( ans != inf ) return ans;
if( k ) {
ans = min( dp( l , r - 1 , 1 ) + ( h[ r ] - h[ r - 1 ] ) * ( n - r + l ) , dp( l , r - 1 , 0 ) + ( h[ r ] - h[ l ] ) * ( n - r + l ) );
} else {
ans = min( dp( l + 1 , r , 1 ) + ( h[ r ] - h[ l ] ) * ( n - r + l ) , dp( l + 1 , r , 0 ) + ( h[ l + 1 ] - h[ l ] ) * ( n - r + l ) );
}
return ans;
}
 
int main() {
freopen( "test.in" , "r" , stdin );
clr( d , inf );
int p;
cin >> n >> p;
rep( i , n ) scanf( "%d" , h + i );
sort( h , h + n );
rep( i , n ) {
int t = f( i , i );
d[ t ][ 0 ] = d[ t ][ 1 ] = abs( p - h[ i ] ) * n;
}
cout << min( dp( 0 , n - 1 , 0 ) , dp( 0 , n - 1 , 1 ) ) << "\n";
return 0;
}

--------------------------------------------------------------

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

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 191  Solved: 102
[Submit][Status][Discuss]

Description

A long, linear field has N (1 <= N <= 1,000) clumps of grass at unique integer locations on what will be treated as a number line. Think of the clumps as points on the number line. Bessie starts at some specified integer location L on the number line (1 <= L <= 1,000,000) and traverses the number line in the two possible directions (sometimes reversing her direction) in order to reach and eat all the clumps. She moves at a constant speed (one unit of distance in one unit of time), and eats a clump instantly when she encounters it. Clumps that aren't eaten for a while get stale. We say the ``staleness'' of a clump is the amount of time that elapses from when Bessie starts moving until she eats a clump. Bessie wants to minimize the total staleness of all the clumps she eats. Find the minimum total staleness that Bessie can achieve while eating all the clumps.

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

Input

输入(ontherun.in) 
输入文件第一行有两个整数,N(N<=3000)和p,分别代表草的堆数和起始位置。下面N行,每行一个整数ai,代表N堆草的位置。(1 <= ai , p <= 1000000)

Output

输出一个整数,最小总腐败值。结果保证在2^31-1内。

Sample Input

Hint

提示 
0时刻,在位置10。 
移动到9,1时刻到达,吃草。 
移动到11,3时刻到达,吃草。 
移动到19,11时刻到达,吃草。 
移动到1,29时刻到达,吃草。 
总腐败值1 + 3 + 11 + 29 = 44最优。 
数据规模 
对于30%的数据,N <= 10。 
对于60%的数据,N <= 300。 
对于100%的数据,N <= 3000。

Input

* Line 1 : Two space-separated integers: N and L. * 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.

HINT

Source

BZOJ 1742: [Usaco2005 nov]Grazing on the Run 边跑边吃草( dp )的更多相关文章

  1. bzoj 1742: [Usaco2005 nov]Grazing on the Run 边跑边吃草【区间dp】

    挺好的区间dp,状态设计很好玩 一开始按套路设f[i][j],g[i][j]为吃完(i,j)区间站在i/j的最小腐败值,后来发现这样并不能保证最优 实际上是设f[i][j],g[i][j]为从i开始吃 ...

  2. bzoj1742[Usaco2005 nov]Grazing on the Run 边跑边吃草*&&bzoj3074[Usaco2013 Mar]The Cow Run*

    bzoj1742[Usaco2005 nov]Grazing on the Run 边跑边吃草 bzoj3074[Usaco2013 Mar]The Cow Run 题意: 数轴上有n棵草,牛初始在L ...

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

    分析: 首先,连续选择一段必定最优... 区间DP,f[i][j]表示从i开始,连续j个被吃掉了,并且,牛在i处,g[i][j]则表示在i+j-1处 f[i][j]可以从g[i+1][j]和f[i+1 ...

  4. 【bzoj1742】[Usaco2005 nov]Grazing on the Run 边跑边吃草 区间dp

    题目描述 John养了一只叫Joseph的奶牛.一次她去放牛,来到一个非常长的一片地,上面有N块地方长了茂盛的草.我们可以认为草地是一个数轴上的一些点.Joseph看到这些草非常兴奋,它想把它们全部吃 ...

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

    数轴上n<=1000个点,从p出发以任意顺序走到所有的点,求到达每个点的时间之和的最小值. 好题!看起来水水的实际易错! 显然的结论是经过一个区间点之后肯定落在左端点或右端点上,谁没事最后还往中 ...

  6. 2018.10.22 bzoj1742: Grazing on the Run 边跑边吃草(区间dp)

    传送门 区间dp入门题. 可以想到当前吃掉的草一定是一个区间(因为经过的草一定会吃掉). 然后最后一定会停在左端点或者右端点. f[i][j][0/1]f[i][j][0/1]f[i][j][0/1] ...

  7. BZOJ1742[Usaco2005 nov]Grazing on the Run

    Description John养了一只叫Joseph的奶牛.一次她去放牛,来到一个非常长的一片地,上面有N块地方长了茂盛的草.我们可 以认为草地是一个数轴上的一些点.Joseph看到这些草非常兴奋, ...

  8. [USACO2005 nov] Grazing on the Run【区间Dp】

    Online Judge:bzoj1742,bzoj1694 Label:区间Dp 题目描述 John养了一只叫Joseph的奶牛.一次她去放牛,来到一个非常长的一片地,上面有N块地方长了茂盛的草.我 ...

  9. BZOJ 1741: [Usaco2005 nov]Asteroids 穿越小行星群

    Description 贝茜想驾驶她的飞船穿过危险的小行星群.小行星群是一个NxN的网格(1≤N≤500),在网格内有K个小行星(1≤K≤10000). 幸运地是贝茜有一个很强大的武器,一次可以消除所 ...

随机推荐

  1. openGL 旋转的图形 矩阵操作

    #include <windows.h> #ifdef __APPLE__ #include <GLUT/glut.h> #else #include <GL/glut. ...

  2. ThinkPHP第十八天(Widget类的使用,连贯操作where IN用法,缓存S函数使用)

    1.Widget类的使用方法: 第一步:在Action同级目录中新建Widget文件夹(独立分组需要自己建立) 第二步:根据不同功能在Widget文件夹中建立不同的Widget类,如热门文章HotWi ...

  3. .net 更改时间格式

    /// <summary> /// 更改时间格式[HH:mm:ss]到[HHmmss] /// </summary> /// <param name="inpu ...

  4. bzoj 1196: [HNOI2006]公路修建问题 二分+并查集

    题目链接 1196: [HNOI2006]公路修建问题 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1576  Solved: 909[Submit ...

  5. [LeetCode]题解(python):096-Unique Binary Search Trees

    题目来源: https://leetcode.com/problems/unique-binary-search-trees/ 题意分析: 给定一个整数n,返回所有中序遍历是1到n的树的可能. 题目思 ...

  6. LintCode-删除元素

    题目描述: 给定一个数组和一个值,在原地删除与值相同的数字,返回新数组的长度. 元素的顺序可以改变,并且对新的数组不会有影响. 样例 给出一个数组 [0,4,4,0,0,2,4,4],和值 4 返回 ...

  7. 使用CarrierWave上传图片时,多版本文件名的统一

    第一次使用CarrierWavewe做上传,不能不说,虽然Rails已经把上传变得超简单了,而CarrierWave则是把上传变成了一种享受,特别是做图片上传,现在这年代,图片展示平台已经不仅仅是电脑 ...

  8. Understanding and Selecting a SIEM/LM: Correlation and Alerting

    Continuing our discussion of core SIEM and Log Management technology, we now move into event correla ...

  9. 模拟美萍加密狗--Rockey2虚拟狗(五)

    虚拟狗开源后很多网友询问有关使用方法的问题,其实看我前四篇文章就应该了解怎样使用了,但还是写篇教程吧 [一].安装DSF (驱动模拟环境): 运行DSFx86Runtime.msi 如需改变安装目录请 ...

  10. 开源项目live555学习心得

      推荐:伊朗美女找丈夫比找工作难女人婚前一定要看清三件事 × 登录注册   疯狂少男-IT技术的博客 http://blog.sina.com.cn/crazyboyzhaolei [订阅][手机订 ...