比赛 20110724 评测结果 C
题目名称 并行 最终得分 0
用户昵称 echo 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2011-07-24 12:59:07
显示代码纯文本
program parellel;
var
   n,i,j,k,l,max,t,min:longint;
   a,b,ans:array[1..10000] of longint;
procedure sorta(l,r:longint);
var
  i,j,mid:longint;
  begin
    i:=l;j:=r;mid:=a[random(j-i)+1];
    repeat
      while a[i]<mid do inc(i);
      while a[j]>mid do dec(j);
      if  not(i>j) then
        begin
          t:=a[i];
          a[i]:=a[j];
          a[j]:=t;
          inc(i);dec(j);
        end;
    until i>j;
    if l<j then sorta(l,j);
    if i<r then sorta(i,r);
  end;
procedure sortb(l,r:longint);
var
  i,j,mid:longint;
  begin
    i:=l;j:=r;mid:=b[random(j-i)+1];
    repeat
      while b[i]<mid do inc(i);
      while b[j]>mid do dec(j);
      if  not(i>j) then
        begin
          t:=b[i];
          b[i]:=b[j];
          b[j]:=t;
          inc(i);dec(j);
        end;
    until i>j;
    if l<j then sortb(l,j);
    if i<r then sortb(i,r);
  end;
function match(x1,y1,x2,y2:longint):longint;
var
  i,j:longint;
  begin
    i:=1;
    j:=1;
    if (x1<y1)or(x2<y2) then
      begin
        exit(0);
      end;
    while (i<=y1)and(j<=y2) do
      begin
        match:=match+abs(a[i]-b[j]);
        i:=i+1;
        j:=j+1;
      end;
  end;

begin
  assign(input,'parellel.in');
  assign(output,'parellel.out');
  reset(input);
  rewrite(output);
  readln(n);
  while n<>0 do
    begin
      max:=maxlongint;
      for i:=1 to n do read(a[i]);
      for i:=1 to n-1 do read(b[i]);
      sorta(1,n);
      sortb(1,n-1);
      for k:=1 to n do
        begin
          min:=maxlongint;
          ans[k]:=match(1,k-1,1,k-1)+match(k+1,n,k,n-1);
          for l:=1 to n-1 do
            if abs(b[l]-a[k])<min then min:=abs(b[l]-a[k]);
          ans[k]:=ans[k]+min;
        end;
      for i:=1 to n do
        if max>ans[i] then max:=ans[i];
      writeln(max);
      readln(n);
    end;
  close(input);
  close(output);
end.