您的位置:首页 > 其它

【NOI2009】 二叉查找树

2012-05-16 10:15 253 查看
treapmod

program treapmod;
uses math;
const maxn=100;
maxnum=1000000000;
type
ty=record
s,q,f:longint;
end;
var
n,k,i:longint;
a:array[0..maxn] of ty;
s:array[0..maxn] of longint;
f:array[0..maxn,0..maxn,0..maxn] of longint;
//==================
procedure sort(l,r,k:longint);
var
i,j,m:longint;
t:ty;
begin
i:=l; j:=r;
if k=1 then m:=a[(l+r) >> 1].q else m:=a[(l+r) >> 1].s;
repeat
if k=1 then
begin
while a[i].q<m do inc(i);
while a[j].q>m do dec(j);
end else
begin
while a[i].s<m do inc(i);
while a[j].s>m do dec(j);
end;
if i<=j then
begin
t:=a[i]; a[i]:=a[j]; a[j]:=t;
inc(i); dec(j);
end;
until i>j;
if i<r then sort(i,r,k);
if j>l then sort(l,j,k);
end;
//==================
function find(i,j,w:longint):longint;
var
x:longint;
begin
if i>j then exit(0);
if f[i,j,w]>-1 then exit(f[i,j,w]);
f[i,j,w]:=maxnum;
for x:=i to j do
begin
if a[x].q>=w then
f[i,j,w]:=min(f[i,j,w],find(i,x-1,a[x].q)+find(x+1,j,a[x].q)+s[j]-s[i-1]);
f[i,j,w]:=min(f[i,j,w],find(i,x-1,w)+find(x+1,j,w)+s[j]-s[i-1]+k);
end;
exit(f[i,j,w]);
end;
//==================
procedure main;
begin
fillchar(f,sizeof(f),255);
writeln(find(1,n,0));
end;
//==================
begin
assign(input,'treapmod.in'); reset(input);
assign(output,'treapmod.out'); rewrite(output);
read(n,k);
for i:=1 to n do read(a[i].s);
for i:=1 to n do read(a[i].q);
for i:=1 to n do read(a[i].f);
sort(1,n,1);
for i:=1 to n do a[i].q:=i;
sort(1,n,2);
for i:=1 to n do s[i]:=s[i-1]+a[i].f;
main;
close(input); close(output);
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: