比赛 20110724 评测结果 AAAAAAAAAA
题目名称 并行 最终得分 100
用户昵称 reamb 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2011-07-24 11:26:44
显示代码纯文本
program bingxing;
var
  min,i,n:longint;
  left,right,a,b:array[0..10001]of longint;
procedure sort1(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
           sort1(l,j);
         if i<r then
           sort1(i,r);
      end;
procedure sort2(l,r: longint);
      var
         i,j,x,y: longint;
      begin
         i:=l;
         j:=r;
         x:=b[(l+r) div 2];
         repeat
           while b[i]<x do
            inc(i);
           while x<b[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=b[i];
                b[i]:=b[j];
                b[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort2(l,j);
         if i<r then
           sort2(i,r);
      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 (a[i]);
      readln;
      for i:=1 to n-1 do
        read (b[i]);
      readln;
      sort1(1,n);
      sort2(1,n-1);
      left[0]:=0;
      right[n+1]:=0;
      for i:=1 to n-1 do
        left[i]:=left[i-1]+abs(a[i]-b[i]);
      for i:=n downto 2 do
        right[i]:=right[i+1]+abs(a[i]-b[i-1]);
      min:=maxlongint;
      for i:=2 to n-1 do
      begin
        if left[i-1]+right[i+1]+abs(a[i]-b[i-1])<min then
          min:=left[i-1]+right[i+1]+abs(a[i]-b[i-1]);
        if left[i-1]+right[i+1]+abs(a[i]-b[i])<min then
          min:=left[i-1]+right[i+1]+abs(a[i]-b[i])
      end;
      if abs(a[1]-b[1])+right[2]<min then
        min:=abs(a[1]-b[1])+right[2];
      if abs(a[n]-b[n-1])+left[n-1]<min then
        min:=abs(a[n]-b[n-1])+left[n-1];
      writeln (min);
      readln (n)
    end;
  close (input);
  close (output)
end.