比赛 20110724 评测结果 AAAAATAATT
题目名称 并行 最终得分 70
用户昵称 donny 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2011-07-24 10:11:49
显示代码纯文本
program parellel;
var
  i,j:longint;
  n:longint;
  p:array[1..10000]of longint;
  r:array[1..10000]of longint;

procedure sort1(x,y:longint);
var
  i,j,k,l:longint;
begin
  i:=x;
  j:=y;
  k:=p[(x+y) div 2];
  repeat
    while p[i]<k do inc(i);
    while p[j]>k do dec(j);
    if i<=j then
    begin
      l:=p[i];
      p[i]:=p[j];
      p[j]:=l;
      inc(i);
      dec(j);
    end;
  until i>j;
  if x<j then sort1(x,j);
  if i<y then sort1(i,y);
end;

procedure sort2(x,y:longint);
var
  i,j,k,l:longint;
begin
  i:=x;
  j:=y;
  k:=r[(x+y) div 2];
  repeat
    while r[i]<k do inc(i);
    while r[j]>k do dec(j);
    if i<=j then
    begin
      l:=r[i];
      r[i]:=r[j];
      r[j]:=l;
      inc(i);
      dec(j);
    end;
  until i>j;
  if x<j then sort2(x,j);
  if i<y then sort2(i,y);
end;

function min(x,y:longint):longint;
begin
  if x<y then exit(x)
  else exit(y);
end;

procedure suan;
var
  i,j,k,l,s,t:longint;
begin
  l:=maxlongint;
  for i:=1 to n do
  begin
    j:=1;
    k:=1;
    s:=0;
    while j<=n do
    begin
      if j=i then inc(j)
      else
      begin
        s:=s+abs(p[j]-r[k]);
        inc(j);
        inc(k);
      end;
    end;
    t:=abs(p[i]-r[1]);
    for j:=2 to n-1 do
      t:=min(t,abs(p[i]-r[j]));
    s:=s+t;
    if s<l then l:=s;
  end;
  writeln(l);
end;

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

  readln(n);
  while n<>0 do
  begin
    for i:=1 to n do
      read(p[i]);
    readln;
    for i:=1 to n-1 do
      read(r[i]);
    readln;

    sort1(1,n);
    sort2(1,n-1);
    suan;

    readln(n);
  end;

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