poj1980

时间:2020-12-01 11:26:34

首先想到费用流,但m<=100000还是算了吧
那就感觉要用dp了,首先将a,b排序
贪心一下可知,a,b的配对肯定不可能出现交叉
这样就可以dp了,复杂度O(nm)还是过不去
在贪心一下会发现,对于a[i],它只可能在j的左右n的范围内匹配(b[j]是b序列中第一个大于a[i]的)
这样就O(n2)了

 type list=array[..] of longint;
var f:array[..,..] of longint;
c,a,b:list;
ans,p,k,i,j,l,r,s,e,n,m:longint; function min(a,b:longint):longint;
begin
if a>b then exit(b) else exit(a);
end; function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end; procedure qsort(var a :list;m:longint);
procedure sort(l,r: longint);
var i,j,x,y: longint;
begin
i:=l;
j:=r;
x:=a[(l+r) div ];
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-;
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end; begin
sort(,m);
end; begin
readln(m,n);
for i:= to m do
readln(b[i]);
for i:= to n do
readln(a[i]);
qsort(a,n);
qsort(b,m);
p:=;
for i:= to n do
begin
while (p<=m) and (a[i]>=b[p]) do inc(p);
c[i]:=p;
end;
k:=;
for i:= to n do
begin
k:=-k;
l:=max(i-,c[i-]-n);
r:=min(m,c[i-]+n);
s:=max(i,c[i]-n);
e:=min(m,c[i]+n);
if i= then r:=m-n+;
f[k,s-]:=;
p:=l;
for j:=s to e do
begin
f[k,j]:=f[k,j-];
while (p<j) and (p<=r) do
begin
f[k,j]:=min(f[k,j],f[-k,p]);
inc(p);
end;
end;
for j:=s to e do
if f[k,j]<> then inc(f[k,j],abs(a[i]-b[j]));
end;
ans:=;
for i:=s to e do
ans:=min(ans,f[k,i]);
writeln(ans);
end.

相关文章