SPSEQ - Sequences
Tác giả: flashmt
Ngôn ngữ: Pascal
const fi='';
fo='';
maxn=100001;
type ar=array[0..maxn] of longint;
var n,re,num,max:longint;
a,d,e,f,inc,dec:ar;
procedure rf;
var i:longint;
begin
assign(input,fi); reset(input);
read(n);
for i:=1 to n do
begin
read(a[i]);
d[i]:=i;
end;
close(input);
end;
procedure sort(l,r:longint);
var i,j,x,y:longint;
begin
i:=l; j:=r; x:=a[(i+j) shr 1];
repeat
while a[i]<x do i:=i+1;
while a[j]>x do j:=j-1;
if i<=j then
begin
y:=a[i]; a[i]:=a[j]; a[j]:=y;
y:=d[i]; d[i]:=d[j]; d[j]:=y;
i:=i+1; j:=j-1;
end;
until i>j;
if i<r then sort(i,r);
if l<j then sort(l,j);
end;
procedure edit;
var i:longint;
begin
a[0]:=-1;
for i:=1 to n do
begin
e[d[i]]:=i;
if a[i]=a[i-1] then d[i]:=d[i-1]
else d[i]:=d[i-1]+1;
end;
max:=d[n];
end;
function getmax(x,y:longint):longint;
begin
if x>y then getmax:=x else getmax:=y;
end;
procedure add(x,t:longint);
begin
while x<=max do
begin
if f[x]<t then f[x]:=t else exit;
x:=x+x and (-x);
end;
end;
function calc(x:longint):longint;
var r:longint;
begin
r:=0;
while x>0 do
begin
r:=getmax(r,f[x]);
x:=x-x and (-x);
end;
calc:=r;
end;
procedure pr;
var i,x,t:longint;
begin
sort(1,n);
edit;
for i:=1 to n do
begin
x:=d[e[i]];
t:=calc(x-1);
inc[i]:=t+1;
add(x,t+1);
end;
fillchar(f,sizeof(f),0);
for i:=n downto 1 do
begin
x:=d[e[i]];
t:=calc(x-1);
dec[i]:=t+1;
add(x,t+1);
end;
re:=0;
for i:=1 to n do
begin
if inc[i]<dec[i] then t:=inc[i] shl 1-1
else t:=dec[i] shl 1-1;
if t>re then re:=t;
end;
end;
procedure wf;
begin
assign(output,fo); rewrite(output);
writeln(re);
close(output);
end;
begin
rf;
pr;
wf;
end.