比赛 20101119 评测结果 WWTTTTTTTE
题目名称 象棋比赛 最终得分 0
用户昵称 maxiem 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2010-11-19 11:07:25
显示代码纯文本
program chess;
var
  table:array [1..5000000] of record
    x,y,v:shortint;
  end;
  data:array [1..5000] of byte;
  flag:array [0..1,1..5000] of boolean;
  ans,t,nn,n,k,i,j:longint;
procedure quicksort(a,b:longint);
var
  s,sx,sy:shortint;
  x,y:longint;
begin
  if a<b then begin
    s:=table[a].v;sx:=table[a].x;sy:=table[a].y;
	x:=a;y:=b;
	repeat
	  while (x<y) and (table[y].v>=s) do dec(y);
	  if x<y then begin
	    table[x]:=table[y];
		inc(x);
	  end;
	  while (x<y) and (table[x].v<=s) do inc(x);
	  if x<y then begin
	    table[y]:=table[x];
		dec(y);
	  end;
	until x=y;
	table[x].v:=s;
	table[x].x:=sx;
	table[x].y:=sy;
	quicksort(a,x-1);
	quicksort(x+1,b);
  end;
end;
procedure add(i,j:integer);
begin
  inc(nn);
  table[nn].x:=i;
  table[nn].y:=j;
  table[nn].v:=abs(data[i]-data[j]);
end;
begin
  assign (input,'chess.in');
  reset (input);
  readln (n,k);
  for i:=1 to n do begin
    readln (data[i]);
	for j:=1 to i-1 do add(i,j);
  end;
  fillchar (flag,sizeof(flag),0);
  close (input);
  assign (output,'chess.out');
  rewrite (output);
  quicksort(1,nn);ans:=0;t:=0;i:=0;
  while t<>k do begin
    inc(i);
    if data[table[i].x]-data[table[i].y]<0 then begin
	  if not(flag[1,table[i].x]) and not(flag[0,table[i].y]) then begin
	    flag[1,table[i].x]:=true;
		flag[0,table[i].y]:=true;
		inc(ans,table[i].v);
		inc(t);
	  end;
	end
	else begin
	  if not(flag[0,table[i].x]) and not(flag[1,table[i].y]) then begin
	    flag[0,table[i].x]:=true;
		flag[1,table[i].y]:=true;
		inc(ans,table[i].v);
		inc(t);
	  end;
	end;
  end;
  writeln (ans);
  close (output);
end.