离散化Pascal模板
A:数的数组
k:数的个数
d[i]:离散化后i-1和i之间的距离
Procedure lsh(var a,d:array of real;k:longint);
Type
rec=record
pos:longint;
num:real;
end;
Var
b:array[0..100000] of rec;
i,now,last:longint;
Procedure qsort(var b:array of rec;l,r:longint);
var
i,j:longint;
x,y:rec;
begin
i:=l;j:=r;x:=b[random(r-l+1)+l];
repeat
while b[i].num<x.num do inc(i);
while b[j].num>x.num do dec(j);
if i<=j then
begin
y:=b[i];
b[i]:=b[j];
b[j]:=y;
inc(i);
dec(j);
end;
until i>j;
if l<j then qsort(b,l,j);
if i<r then qsort(b,i,r);
end;
begin
for i:=1 to k do
with b[i] do
begin
num:=a[i];
pos:=i;
end;
qsort(b,1,k);
now:=1;
d[1]:=b[1].num;
a[b[1].pos]:=1;
last:=1;
for i:=2 to k do
begin
if b[i-1].num<>b[i].num then
begin
d[now+1]:=b[i].num-b[last].num;
last:=i;
inc(now);
end;
a[b[i].pos]:=now;
end;
end;

浙公网安备 33010602011771号