1. 程式人生 > >NOIP賽前常用模板程式碼總結

NOIP賽前常用模板程式碼總結

1:堆程式碼(以小根堆作為example)

procedure put(x:longint);
var e:longint;
begin
	inc(len); tree[len]:=x;
	e:=len;
	while e>1 do 
	begin
		if tree[e>>1]>tree[e] then 
		begin
			swap(tree[e>>1],tree[e]);
			e:=e>>1;
		end
		else break;
	end;
end;

function get:longint;
var e,son:longint;
begin
	get:=tree[1]; 
	tree[1]:=tree[len]; dec(len);
	e:=1;
	while (e<<1<=len) or (e<<1+1<=len) do 
	begin
		if (e<<1+1>len) or (tree[e<<1]<tree[e<<1+1]) then son:=e<<1
		else son:=e<<1+1;
		if tree[e]>tree[son] then 
		begin
			swap(tree[e],tree[son]);
			e:=son;
		end
		else break;
	end;
end;