const
maxn=200005;
var
x,y,z,next:array[-2*maxn..2*maxn]of longint;
seg:array[0..4*maxn,1..4]of longint;
i,j,k:longint;
n,m,t1,t2,ans:longint;
functionmax(a,b:longint):longint;
beginif a>b thenexit(a) elseexit(b); end;
procedurebuild(a,l,r:longint);var mid:longint;
begin
seg[a,1]:=l; seg[a,2]:=r; seg[a,3]:=0; seg[a,4]:=l;
if l=r thenexit;
mid:=(l+r)>>1;
build(a<<1,l,mid); build(a<<1+1,mid+1,r);
end;
procedureupdate(a,b,c:longint);var mid:longint;
beginif seg[a,1]=seg[a,2] thenbegin seg[a,3]:=c; exit; end;
mid:=(seg[a,1]+seg[a,2])>>1;
if b<=mid then update(a<<1,b,c) else update(a<<1+1,b,c);
if seg[a<<1,3]>seg[a<<1+1,3]
thenbegin seg[a,3]:=seg[a<<1,3]; seg[a,4]:=seg[a<<1,4]; endelseif seg[a<<1,3]<seg[a<<1+1,3]
thenbegin seg[a,3]:=seg[a<<1+1,3]; seg[a,4]:=seg[a<<1+1,4]; endelsebegin seg[a,3]:=seg[a<<1+1,3]; seg[a,4]:=seg[a<<1,4]; end;
end;
begin
readln(n,m);
for i:=1to n do
y[i]:=-i;
for i:=1to n dobeginread(x[i]);
next[y[x[i]]]:=i;
y[x[i]]:=i;
end;
for i:=-n to n doif next[i]=0then next[i]:=n+1;
build(1,1,n);
for i:=1to m dobegin
y[i]:=1;
update(1,i,next[-i]);
z[i]:=next[-i];
end;
ans:=0;
for i:=1to n dobeginif y[x[i]]=1thenbegin update(1,x[i],next[z[x[i]]]); z[x[i]]:=next[z[x[i]]]; continue; end;
inc(ans);
t1:=seg[1,3]; t2:=seg[1,4];
if t1>next[i]
thenbegin update(1,t2,0); update(1,x[i],next[i]); z[x[i]]:=next[i]; y[t2]:=0; y[x[i]]:=1; end;
end;
writeln(ans);
end.
T2
uses math;
var
x,p,q:array[0..20050]of longint;
i,j,k:longint;
n,l,r,mid,ans,s:longint;
begin
assign(input,'sbtwo.in'); assign(output,'sbtwo.out'); reset(input); rewrite(output);
readln(n);
for i:=1 to n do
readln(x[i]);
s:=x[n];
for i:=1 to n-1dos:=max(s,x[i]+x[i+1]);
l:=s; r:=2*s;
while l<r do
begin
mid:=(l+r)>>1;
p[1]:=x[1]; q[1]:=x[1];
for i:=2 to n do
begin
p[i]:=max(0,x[i]+x[i-1]+x[1]-q[i-1]-mid);
q[i]:=min(x[i],x[1]-p[i-1]);
end;
if p[n]=0
then r:=mid
else l:=mid+1;
end;
writeln(l);
close(input); close(output);
end.