记录编号 24680 评测结果 AAAAAAAAAA
题目名称 [USACO Jan09] 气象牛 最终得分 100
用户昵称 Gravatarybh 是否通过 通过
代码语言 Pascal 运行时间 0.129 s
提交时间 2011-04-14 17:54:21 内存使用 0.21 MiB
显示代码纯文本
program baric;
var
  f:array[0..110,0..110] of int64;
  sum,a:array[0..110] of longint;
  n,e,i,j,k,p,temp:longint;

function min(x,y:int64):int64;
begin
  if x<y then min:=x else min:=y;
end;

begin
  assign(input,'baric.in');
  reset(input);
  assign(output,'baric.out');
  rewrite(output);

  readln(n,e);
  sum[0]:=0;
  for i:=1 to n do
  begin
    read(a[i]);
    sum[i]:=sum[i-1]+a[i];
  end;

  for i:=1 to n do
  begin
    f[i,1]:=0;
    for j:=1 to i-1 do
      f[i,1]:=f[i,1]+2*abs(a[j]-a[i]);
  end;

  for j:=2 to n do
    for i:=j to n do
    begin
      f[i,j]:=maxlongint;
      for k:=j-1 to i-1 do
      begin
        temp:=0;
        for p:=k+1 to i-1 do
          temp:=temp+abs(2*a[p]-a[i]-a[k]);
        f[i,j]:=min(f[i,j],f[k,j-1]+temp);
      end;
    end;

  for j:=2 to n+1 do
  begin
    f[n+1,j]:=maxlongint;
    for i:=j-1 to n do
    begin
      temp:=0;
      for p:=i+1 to n do
        temp:=temp+2*abs(a[p]-a[i]);
      f[n+1,j]:=min(f[n+1,j],f[i,j-1]+abs(temp));
    end;
    if f[n+1,j]<=e then
    begin
      writeln(j-1,' ',f[n+1,j]);
      break;
    end;
  end;

  close(input);
  close(output);
end.