比赛 20111021 评测结果 AAAAAAAATA
题目名称 地图着色 最终得分 90
用户昵称 reamb 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2011-10-21 21:52:36
显示代码纯文本
program cowyotz;
var
  f:array[1..3000,1..10]of int64;
  a:array[1..3000]of longint;
  sum:array[0..3000]of int64;
  i,j,k,mid,n,m:longint;
procedure sort(l,r: longint);
      var
         i,j,x,y: longint;
      begin
         i:=l;
         j:=r;
         x:=a[(l+r) div 2];
         repeat
           while a[i]<x do
            inc(i);
           while x<a[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=a[i];
                a[i]:=a[j];
                a[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;
begin
  assign (input,'map.in');
  reset (input);
  assign (output,'map.out');
  rewrite (output);
    readln (n);
    readln (m);
    for i:=1 to n do
      readln (a[i]);
    sort(1,n);
    for i:=1 to n do
      sum[i]:=sum[i-1]+a[i];
    for i:=1 to n do
      for j:=1 to m do
        f[i,j]:=maxlongint;
    for i:=1 to n do
    begin
      mid:=(i+1)div 2;
      f[i,1]:=abs((mid-1)*a[mid]-sum[mid-1])+abs((i-mid)*a[mid]-(sum[i]-sum[mid]));
    end;
    for i:=2 to n do
      for j:=2 to m do
        if i>=j then
        begin
          for k:=1 to i-1 do
          begin
            if f[k,j-1]<>maxlongint then
            begin
              mid:=(k+1+i)div 2 ;
              if f[k,j-1]+abs((mid-k-1)*a[mid]-sum[mid-1]+sum[k])
              +abs((i-mid)*a[mid]-sum[i]+sum[mid])<f[i,j] then
                f[i,j]:=f[k,j-1]+abs((mid-k-1)*a[mid]-sum[mid-1]+sum[k])
              +abs((i-mid)*a[mid]-sum[i]+sum[mid])
            end
          end
        end;
    writeln (f[n,m]);
  close (input);
  close (output)
end.