2017年衢州联赛 T2
题目
题解
单调栈或单调序列 + 二分查找优化
代码(Pascal)
var n,tot,ans:longint;
a,q:array[0..50005]of longint;
procedure init;
var i,x:longint;
begin
assign(input,'skyline.in');reset(input);
assign(output,'skyline.out');rewrite(output);
readln(n);
for i:=1 to n do
readln(x,a[i]);
end;
function find(x:longint):longint;
var l,r,mid:longint;
begin
l:=0;r:=tot;
while l<=r do
begin
mid:=(r+l+1) div 2;
if q[mid]<x then begin
find:=mid;
l:=mid+1;
end
else r:=mid-1;
end;
end;
procedure main;
var i,j,t:longint;
begin
tot:=0;ans:=0;
for i:=1 to n do
begin
if a[i]=0 then begin
tot:=0;
continue;
end;
t:=find(a[i]);
if t=tot then begin
inc(ans);inc(tot);
q[tot]:=a[i];
end else
if q[t+1]=a[i] then tot:=t+1
else begin
inc(ans);
tot:=t+1;
q[tot]:=a[i];
end;
end;
end;
procedure print;
begin
write(ans);
close(input);close(output);
end;
begin
init;
main;
print;
end.