Назад к лучшим решениям Status: AC, problem ZFIRM, contest ZEL07. By extremall (Naumenko Alexander), 2007-02-16 08:30:32.
var n,m:integer;
a:array[1..5000] of integer;
p:array[0..5000] of integer;
c:array[0..5000] of integer;
c1,c2,pr:integer;
procedure vod;
var i,j,z,k:integer;
begin
readln(n);
for i:=1 to n do read(a[i]);
c[0]:=0;
p[0]:=0;
c1:=0;
c2:=0;
pr:=0;
for i:=1 to n do begin
if a[i]<>0 then begin
inc(c[0]);
j:=i;
k:=0;
while a[j]<>0 do begin
inc(p[0]);
inc(k);
p[p[0]]:=a[j];
z:=a[j];
a[j]:=0;
j:=z;
end;
c[c[0]]:=k;
c1:=c1+(k div 2);
if k mod 2=0 then inc(c2);
if k=2 then inc(pr);
end;
end;
z:=0;
if c1-c2+pr<=0 then begin write(0);halt;end;
if c1-pr>0 then writeln(2) else writeln(1);
write(c1-c2+pr);
for i:=1 to c[0] do begin
if c[i]>2 then begin
if c[i] mod 2=1 then begin
write(' ',p[c[i]+z],'-',p[1+z]);
for j:=1 to (c[i] div 2)-1 do
write(' ',p[j+z+1],'-',p[c[i]+z-j]);
end
else begin
for j:=1 to (c[i] div 2)-1 do
write(' ',p[j+z+1],'-',p[c[i]+z-j+1]);
end;
end
else if c[i]=2 then begin
write(' ',p[z+1],'-',p[z+2]);
end;
z:=z+c[i];
end;
if c1-pr>0 then begin
writeln;
write(c1-pr);
z:=0;
for i:=1 to c[0] do begin
if c[i]>2 then begin
if c[i] mod 2=1 then begin
for j:=1 to c[i] div 2 do
write(' ',p[j+z],'-',p[c[i]+z-j]);
end
else begin
for j:=1 to c[i] div 2 do
write(' ',p[j+z],'-',p[c[i]+z-j+1]);
end;
end;
z:=z+c[i];
end;
end;
end;
begin
vod;
//readln;readln;
end.