# [BZOJ1694/1742/3074]The Cow Run 三倍经验

## Description

John养了一只叫Joseph的奶牛。一次她去放牛，来到一个非常长的一片地，上面有N块地方长了茂盛的草。我们可

## 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:
* 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

// By BriMon
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int res=0;char ch=getchar();
while(!isdigit(ch)) ch=getchar();
while(isdigit(ch)){res=(res<<3)+(res<<1)+(ch^48);ch=getchar();}
return res;
}

int n, x;
int f[1005][1005][2], pos[1005];

int main()
{
for (int i = 1 ; i <= n ; i ++)
sort(pos + 1, pos + 1 + n);
memset(f, 0x3f, sizeof f);
for (int i = 1 ; i <= n ; i ++) f[i][i][0] = f[i][i][1] = abs(x - pos[i]) * n;
for (int len = 2 ; len <= n ; len ++)
{
for (int i = 1 ; i <= n ; i ++)
{
int j = i + len - 1;
if (j > n) break;
f[i][j][0] = min(f[i][j][0], min(f[i+1][j][1] + (n - (j - i)) * (pos[j] - pos[i]), f[i+1][j][0] + (n - (j - i)) * (pos[i+1] - pos[i])));
f[i][j][1] = min(f[i][j][1], min(f[i][j-1][1] + (n - (j - i)) * (pos[j] - pos[j-1]), f[i][j-1][0] + (n - (j - i)) * (pos[j] - pos[i])));
}
}
printf("%d\n", min(f[1][n][1], f[1][n][0]));
return 0;
}

posted @ 2018-08-03 17:45  zZhBr  阅读(136)  评论(0编辑  收藏  举报