Pagini recente » Cod sursa (job #1554636) | Cod sursa (job #290939) | Cod sursa (job #2536544) | Cod sursa (job #928981) | Cod sursa (job #1571790)
var i,j,n,k,m:longint;
a,b,c,d:array[0..100000] of longint;
procedure caut(k,st,dr:longint);
var j:longint;
begin
if st=dr then begin
b[st]:=a[k];
c[k]:=st;
end else begin
j:=(st+dr) div 2;
if b[j]<a[k] then caut(k,j+1,dr)
else caut(k,st,j);
end;
end;
begin
assign(input,'scmax.in');
assign(output,'scmax.out');
reset(input);
rewrite(output);
read(n);
for i:=1 to n do read(a[i]);
b[1]:=a[1];
c[1]:=1;
k:=1;
for i:=2 to n do begin
if a[i]>b[k] then begin
inc(k);
b[k]:=a[i];
c[i]:=k;
end else
caut(i,1,k);
end;
// for i:=1 to k do write(b[i],' ');
// writeln;
// for i:=1 to n do write(c[i],' ');
// writeln;
m:=k;
for i:=n downto 1 do begin
if (c[i]=m) and (k=m) then begin
d[k]:=a[i];
dec(k)
end;
if (c[i]=k) and (d[k+1]>a[i]) then begin
d[k]:=a[i];
dec(k);
end;
end;
writeln(m);
for i:=1 to m do write(d[i], ' ');
end.