比赛 20110724 评测结果 AAAAAAAAAA
题目名称 并行 最终得分 100
用户昵称 ybh 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2011-07-24 11:08:23
显示代码纯文本
program parellel;
const
  maxn=20000;
var
  a,p:array[0..maxn] of longint;
  f1,f2:array[0..maxn] of int64;
  n,i,j:longint;
  ans:int64;
  
procedure sort(l,r:longint);
var
  i,j,x,y:longint;
begin
  x:=a[(l+r) div 2];
  i:=l;
  j:=r;
  repeat
    while a[i]<x do inc(i);
    while a[j]>x do dec(j);
    if i<=j then
    begin
      y:=a[i]; a[i]:=a[j]; a[j]:=y;
      inc(i); dec(j);
    end;
  until i>j;
  if l<j then sort(l,j);
  if i<r then sort(i,r);
end;

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

  read(n);
  while n>0 do
  begin
    for i:=1 to n do
      read(a[i]);
    for i:=1 to n-1 do
      read(a[n+i]);
    sort(1,n);
    sort(n+1,n+n-1);
    
    fillchar(p,sizeof(p),0);
    j:=n+1;
    i:=1;
    while a[j]>a[i] do
    begin
      p[i]:=maxlongint;    
      inc(i);
    end;
    for i:=i to n do
    begin
      while (j<n+n-1) and (a[j+1]<a[i]) do
        inc(j);
      p[i]:=a[i]-a[j];
    end;
    
    j:=n+n-1;
    i:=n;
    while a[j]<a[i] do
      dec(i);
    for i:=i downto 1 do
    begin
      while (j>n+1) and (a[j-1]>a[i]) do
        dec(j);
      if a[j]-a[i]<p[i] then p[i]:=a[j]-a[i];
    end;
      
    f1[0]:=0;
    f2[n+1]:=0;
    for i:=1 to n-1 do
      f1[i]:=f1[i-1]+abs(a[i]-a[n+i]);
    for i:=n downto 2 do
      f2[i]:=f2[i+1]+abs(a[i]-a[n-1+i]);
    
    ans:=500000000;
    for i:=1 to n do
      if f1[i-1]+f2[i+1]+p[i]<ans then ans:=f1[i-1]+f2[i+1]+p[i];
    writeln(ans);
    
    read(n);
  end;
  
  close(input);
  close(output);
end.