比赛 20111021 评测结果 AAAAAAAAAA
题目名称 地图着色 最终得分 100
用户昵称 Des. 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2011-10-21 20:00:47
显示代码纯文本
program map;
var a:Array[1..3000]of longint;
    sum:array[1..3000,0..3000]of longint;
    f:array[1..3000,0..20]of int64;
    t,k,m,n,i,j:longint;
function min(a,b:int64):int64;
begin
if a>b then exit(b) else exit(a);
end;
procedure qsort(l,r:longint);
var i,j,k,m,c:longint;
begin
k:=(l+r)div 2;
m:=a[k];
i:=l;
j:=r;
repeat
  while a[i]<m do inc(i);
  while a[j]>m do dec(j);
  if i<=j then
    begin
      c:=a[i];
      a[i]:=a[j];
      a[j]:=c;
      inc(i);
      dec(j);
    end;
until i>j;
if i<r then qsort(i,r);
if l<j then qsort(l,j);
end;
begin
assign(input,'map.in');
reset(input);
assign(output,'map.out');
rewrite(output);
readln(n);
readln(m);
for t:=1 to n do
  readln(a[t]);
qsort(1,n);
for i:=1 to n do
  for j:=i to n do
    sum[i,j]:=sum[i,j-1]+a[j]-a[j-(j-i+1)div 2];
for i:=1 to n do
  for j:=1 to m do
    f[i,j]:=maxlongint*10000;
f[1,1]:=0;
for i:=1 to n do
  f[i,0]:=sum[1,i];
for j:=1 to m-1 do
  for i:=j to n do
    for k:=j to i-1 do
      f[i,j]:=min(f[i,j],f[k,j-1]+sum[k+1,i]);
writeln(f[n,m-1]);
close(output);
end.