【2011noip提高】选择客栈
不会O(n)……只好用线段树O(nlogn)
uses math;
var n,k,p,i,j,c,ans,minp,tot:longint;
co:array[0..50,0..50000] of longint;
f:array[0..600000] of longint;
a:array[0..200000] of longint;
procedure buildtree(l,r,k:longint);
var mid:longint;
begin
if l=r then
begin
f[k]:=a[l];
exit;
end;
mid:=(l+r) shr 1;
buildtree(l,mid,k shl 1);
buildtree(mid+1,r,k shl 1+1);
f[k]:=min(f[k shl 1],f[k shl 1+1]);
end;
procedure find(l1,r1,l2,r2,k:longint);
var mid:longint;
begin
if (l1=l2)and(r1=r2) then
begin
minp:=min(minp,f[k]);
exit;
end;
mid:=(l2+r2) shr 1;
if r1<=mid then
begin
find(l1,r1,l2,mid,k shl 1);
exit;
end;
if l1>mid then
begin
find(l1,r1,mid+1,r2,k shl 1+1);
exit;
end;
find(l1,mid,l2,mid,k shl 1);
find(mid+1,r1,mid+1,r2,k shl 1+1);
end;
begin
read(n,k,p);
for i:=1 to n do
begin
read(c,a[i]);
inc(co[c,0]);
co[c,co[c,0]]:=i;
end;
ans:=0;
buildtree(1,n,1);
for i:=0 to k-1 do
begin
tot:=0;
for j:=2 to co[i,0] do
begin
minp:=100;
find(co[i,j-1],co[i,j],1,n,1);
if minp<=p then tot:=j-1;
inc(ans,tot);
end;
end;
write(ans);
end.
奇技淫巧……
(学好数据结构,走遍天下都不怕!)