牛棚

牛棚


Time Limits: 1000 ms Memory Limits: 65536 KB Detailed Limits
Description
译题:
  FJ有N(2<=N<=1,500)头牛编号为1到N,FJ新盖了S(N<=S<=1,000,000)个牛棚,编号为1到S,S个牛棚排成一排,相邻牛棚距离为1。
每个牛棚只能住一头牛,每头牛都选择了一个牛棚P_i来休息,当两头牛离得太近时就会变得很暴躁,FJ想移动一些牛到其他牛棚使得他们之间的间距尽可能大,同时FJ又希望这N-1个间距尽可能相似。
具体一点说,FJ希望所有间距与(S-1)DIV(N-1)最多相差1,而且希望这N-1个间距尽可能多的等于(S-1)DIV(N-1)。例如4个牛住8个牛棚,可以有以下方案:1,3,5,8或1,3,6,8,但1,2,4,7或1,2,4,8是不符合要求的。
帮助FJ用最少的移动距离满足要求。

原题:
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.

Input

  • Line 1: Two space-separated integers: N and S
  • Lines 2..N+1: Line i+1 contains the single integer: P_i

  第1行:两个空格隔开的整数N和S
  第2到N+1行:第i+1行包含一个整数P_i

Output

  • 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).  

输出一个整数表示最少移动距离。

Sample Input
5 10
2
8
1
3
9

Sample Output
4

Hint
【样例说明】

V12345678910
Init StallABC....DE.
Final StallA.B.C..D.E
Distance moved0.1.2..0.1

题目补充

这道题可能在翻译的过程中有部分的缺陷,对于奶牛的分布,要求在第1和第s个牛棚上,都有一只奶牛,其次题目给出一个X=(s-1)div(n-1),使相邻的两个奶牛的距离最小是X最大是X+1,又因为首尾都有奶牛,所以在n奶牛形成的n-1个空隙中Y=(s-1)mod(n-1)个的长度是X+1,其余都是X。


解题思路

先求出X=(s-1)div(n-1) Y=(s-1)mod(n-1)
然后排序。
Fi,j 表示我们已经放置了前i头牛,有j段空隙的距离为X+1的最小移动距离。
那么转移方程明显就是
Fi,j=Min(Fi1,j,Fi1,j1)+|(i1)x+j+1Ai|
其中 (2<=i<=n,0<=j<=Min(Y,i1)) ,除了 F1,0=A[1]1 外F数组初值都用fillchar赋为$7f。

那么 Fn,Y

Codes:
var a:Array[1..1500]of longint;
    f:Array[1..1500,-1..1500]of longint;
    D,n,i,j,x,s,y,l:longint;
procedure swap(var a,b:longint);
    var c:longint;
begin
    c:=a; a:=b; b:=c;
end;
procedure qsort(h,t:longint);
    var l,r,m:longint;
begin
    l:=h; r:=t;
    m:=a[(h+t)>>1];
    repeat
        while a[l]<m do inc(l);
        while m<a[r] do dec(r);
        if l<=r then
        begin
            swap(a[l],a[r]);
            inc(l); dec(r);
        end;
    until l>r;
    if h<r then qsort(h,r);
    if l<t then qsort(l,t);
end;
function min(p,q:longint):longint;
begin
    if p<q then exit(p) else exit(q);
end;
begin
    read(n,s);
    for i:=1 to n do read(a[i]);
    qsort(1,n);
    fillchar(f,sizeof(f),$7f);
    f[1,0]:=a[1]-1;
    x:=(s-1)div(n-1);
    y:=(s-1)mod(n-1);
    for i:=2 to n do
    begin
        s:=min(i-1,y);
        f[i,0]:=f[i-1,0]+abs(a[i]-i*x+x-1);
        for j:=1 to s do f[i,j]:=min(f[i-1,j-1],f[i-1,j])+abs(a[i]-i*x+x-j-1);
    end;
    writeln(f[n,y]);
end.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值