A22509.Grazing2 S

普及+/提高

USACO

通过率:0%

时间限制:1.00s

内存限制:128MB

题目描述

Farmer John has N (2 <= N <= 1,500) prize milk cows conveniently numbered 1..N. His newly-painted barn has S (N <= S <= 1,000,000) stalls (conveniently numbered 1..S) in a single long line; each stall is a unit distance from its neighboring stall(s).

The cows have made their way to the stalls for a rest; cow i is in stall P_i. Antisocial as they are, the cows get grumpy if they are situated in stalls very close to each other, so Farmer John wants to move the cows to be as spread out as possible.

FJ wants to make sure that the N - 1 distances between adjacent cows are as large as possible, and he would also like them to be similar to each other (i.e., close to equi-distant spacing).

In particular, FJ would like all distances between adjacent cows to be at most 1 different from (S - 1) / (N - 1), where integer division is used. Moreover, he would like as many of these distances as possible to be exactly equal to (S - 1) / (N - 1) [integer

division]. Thus, with four cows and eight stalls, one can place the cows at positions 1, 3, 5, 8 or 1, 3, 6, 8 but not at 1, 2, 4, 7 or 1, 2, 4, 8.

Help FJ spread the cows as efficiently as possible by calculating and reporting the minimum total distance that the cows have to move in order to achieve proper spacing. Ignore the distance it takes for a cow to enter or exit a stall.

约翰有N(2≤N≤1500)头奶牛,S(N≤S≤1,000,000)个一字排开的牛棚.相邻牛棚间的距离恰好为1.

奶牛们已经回棚休息,第i只奶牛现在待在牛棚Pi.如果两只奶牛离得太近,会让奶牛们变得很暴躁.所以约翰想给一些奶牛换一个棚,让她们之间的距离变得尽量大,并且尽管接近.令d=Trunc((s-1)/(n-1))

所以约翰希望最终的奶牛的状态是:两只相邻奶牛间的距离与d之差不超过1,而且让尽量多的间距等于d.因此,对于4只奶牛8个棚的情况,1,3,5,8或1,3,6,8这样的安置情况是允许的,而1,2,4,7或1,2,4,8这样的情况是不允许的. 帮助约翰移动奶牛,让所有奶牛的移动距离之和最小,同时让最终的安置情况符合约翰心意.

输入格式

* Line 1: Two space-separated integers: N and S

* Lines 2..N+1: Line i+1 contains the single integer: P_i

输出格式

* Line 1: A single integer: the minimum total distance the cows have to travel. This number is guaranteed to be under 1,000,000,000 (thus fitting easily into a signed 32-bit integer).

输入输出样例

  • 输入#1

    5 10 
    2 
    8 
    1 
    3 
    9 
    

    输出#1

    4 
    

说明/提示

1 2 3 4 5 6 7 8 9 10

Cow Locs | A | B | C | . | . | . | . | D | E | . |

Cows move from stall 2 to 3, 3 to 5, and 9 to 10. The total distance moved is 1 + 2 + 1 = 4. The final positions of the cows are in stalls 1, 3, 5, 8, and 10.

1 2 3 4 5 6 7 8 9 10

Init Stall | A | B | C | . | . | . | . | D | E | . |
Final Stall | A | . | B | . | C | . | . | D | . | E |
Distance moved | 0 | . | 1 | . | 2 | . | . | 0 | . | 1 |

首页