Pagini recente » Diferente pentru problema/bitsort intre reviziile 5 si 8 | Cod sursa (job #22129) | Cod sursa (job #167701) | Cod sursa (job #180801) | Cod sursa (job #71099)
Cod sursa(job #71099)
program sir_;
const v=100000;
type sir=array[1..v]of longint;
var a,minim,maxim:sir;
s,t,pp,uu,p,u,n,x,y,z,l,lung,inceput,sf:longint;
procedure citire;
var i:longint;
begin
assign(input,'sir.in');
reset(input);
read(n,x,y,z);
for i:=1 to n do
read(a[i]);
close(input);
end;
procedure min(i:longint);
begin
inc(u);
if u>v then
u:=1;
minim[u]:=i;
while (a[minim[u]]<a[minim[u-1]]) and (u<>p) do
begin
if u-1<1 then
begin
minim[v]:=minim[u];
u:=v;
end else begin
minim[u-1]:=minim[u];
dec(u);
end;
end;
if (minim[u]-minim[p]+1>y) then
inc(p);
end;
procedure max(i:longint);
begin
inc(uu);
if uu>v then
uu:=1;
maxim[uu]:=i;
while (a[maxim[uu]]>a[maxim[uu-1]]) and (uu<>pp) do
begin
if uu-1<1 then
begin
maxim[v]:=maxim[uu];
uu:=v;
end else begin
maxim[uu-1]:=maxim[uu];
dec(uu);
end;
end;
if (maxim[uu]-maxim[pp]+1>y) then
inc(pp);
end;
procedure verificare;
var i:longint;
begin
for i:=s to t do
if (a[i]>=a[maxim[pp]]) or (a[i]<=a[minim[p]]) then
begin
l:=-1;
exit;
end;
end;
procedure lungime;
begin
if (a[maxim[pp]]-a[minim[p]]<=z) and (a[maxim[pp]]-a[minim[p]]>=0) then
begin
// l:=abs(minim[p]-maxim[pp])+1;
if minim[p]<maxim[pp] then
begin
s:=minim[p];
t:=maxim[pp];
end else begin
t:=minim[p];
s:=maxim[pp];
end;
l:=t-s+1;
verificare;
if l>=0 then begin
while (l+1<=y) and (s-1>=1) and (a[s-1]>=a[minim[p]]) and (a[s-1]<=a[maxim[pp]]) do
begin
dec(s);
inc(l);
end;
while (l+1<=y) and (t+1<=n) and (a[t+1]>=a[minim[p]]) and (a[t+1]<=a[maxim[p]]) do
begin
inc(t);
inc(l);
end;
if (l>lung) and (l>=x) and (l<=y) then
begin
lung:=l;
inceput:=s;
sf:=t;
end else if (l=lung) and (s<inceput) then
begin
lung:=l;
inceput:=s;
sf:=t;
end
end; end;
end;
procedure rezolvare;
var i:longint;
begin
minim[1]:=1;
p:=1;
u:=1;
maxim[1]:=1;
pp:=1;
uu:=1;
for i:=2 to n do
begin
min(i);
max(i);
lungime;
end;
end;
procedure scriere;
begin
assign(output,'sir.out');
rewrite(output);
if inceput=-1 then
writeln(inceput)
else writeln(lung,' ',inceput,' ',sf);
close(output);
end;
begin
citire;
inceput:=-1;
lung:=0;
rezolvare;
scriere;
end.