比赛 20110724 评测结果 AAAAAWAAWT
题目名称 并行 最终得分 70
用户昵称 wo shi 刘畅 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2011-07-24 12:31:48
显示代码纯文本
type
  jp=array[0..100000]of longint;

var
  n,i,j,now,ans,min,t,time,long,x,y,s:longint;
  a,b,p:jp;

procedure qsort(var a:jp; l,r:longint);

procedure sort(l,r:longint);
var
  x,y,i,j: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 i<=j then
    begin
      y:=a[i];
      a[i]:=a[j];
      a[j]:=y;
      inc(i);
      dec(j);
    end;
  until i>j;
  if i<r then sort(i,r);
  if l<j then sort(l,j);
end;

begin
  sort(l,r);
end;

begin
  assign(input,'parellel.in'); reset(input);
  assign(output,'parellel.out'); rewrite(output);
  readln(n);
  randomize;
  repeat
    for i:=1 to n do read(a[i]);
    for i:=1 to n-1 do read(b[i]);
    qsort(a,1,n);
    qsort(b,1,n-1);
    for i:=1 to n do p[i]:=i;
    for time:=1 to 3000000 div n do
    begin
      x:=random(n)+1;
      y:=random(n)+1;
      t:=p[x];
      p[x]:=p[y];
      p[y]:=t;
    end;
    ans:=maxlongint;
    if 1000<n then long:=1000
    else long:=n;
    for time:=1 to long do
    begin
      s:=p[time];
      j:=1;
      now:=0;
      for i:=1 to s-1 do
      begin
        inc(now,abs(a[i]-b[j]));
        inc(j);
      end;
      for i:=s+1 to n do
      begin
        inc(now,abs(a[i]-b[j]));
        inc(j);
      end;
      min:=maxlongint;
      for j:=1 to n-1 do
      if abs(a[s]-b[j])<min then min:=abs(a[s]-b[j]);
      inc(now,min);
      if now<ans then ans:=now;
    end;
    writeln(ans);
    readln(n);
  until n=0;
  close(input);
  close(output);
end.