Назад к лучшим решениям Status: AC, problem ZFIRM, contest ZEL07. By extremall (Naumenko Alexander), 2007-02-16 08:30:32.
  1. var n,m:integer;
  2. a:array[1..5000] of integer;
  3. p:array[0..5000] of integer;
  4. c:array[0..5000] of integer;
  5. c1,c2,pr:integer;
  6.  
  7. procedure vod;
  8. var i,j,z,k:integer;
  9. begin
  10. readln(n);
  11. for i:=1 to n do read(a[i]);
  12. c[0]:=0;
  13. p[0]:=0;
  14. c1:=0;
  15. c2:=0;
  16. pr:=0;
  17. for i:=1 to n do begin
  18. if a[i]<>0 then begin
  19. inc(c[0]);
  20. j:=i;
  21. k:=0;
  22. while a[j]<>0 do begin
  23. inc(p[0]);
  24. inc(k);
  25. p[p[0]]:=a[j];
  26. z:=a[j];
  27. a[j]:=0;
  28. j:=z;
  29. end;
  30. c[c[0]]:=k;
  31. c1:=c1+(k div 2);
  32. if k mod 2=0 then inc(c2);
  33. if k=2 then inc(pr);
  34. end;
  35. end;
  36. z:=0;
  37. if c1-c2+pr<=0 then begin write(0);halt;end;
  38. if c1-pr>0 then writeln(2) else writeln(1);
  39. write(c1-c2+pr);
  40. for i:=1 to c[0] do begin
  41. if c[i]>2 then begin
  42. if c[i] mod 2=1 then begin
  43. write(' ',p[c[i]+z],'-',p[1+z]);
  44. for j:=1 to (c[i] div 2)-1 do
  45. write(' ',p[j+z+1],'-',p[c[i]+z-j]);
  46. end
  47. else begin
  48. for j:=1 to (c[i] div 2)-1 do
  49. write(' ',p[j+z+1],'-',p[c[i]+z-j+1]);
  50. end;
  51. end
  52. else if c[i]=2 then begin
  53. write(' ',p[z+1],'-',p[z+2]);
  54. end;
  55. z:=z+c[i];
  56. end;
  57. if c1-pr>0 then begin
  58. writeln;
  59. write(c1-pr);
  60. z:=0;
  61. for i:=1 to c[0] do begin
  62. if c[i]>2 then begin
  63. if c[i] mod 2=1 then begin
  64. for j:=1 to c[i] div 2 do
  65. write(' ',p[j+z],'-',p[c[i]+z-j]);
  66. end
  67. else begin
  68. for j:=1 to c[i] div 2 do
  69. write(' ',p[j+z],'-',p[c[i]+z-j+1]);
  70. end;
  71. end;
  72. z:=z+c[i];
  73. end;
  74. end;
  75. end;
  76.  
  77. begin
  78. vod;
  79. //readln;readln;
  80. end.