fork download
  1. program paletta;
  2. var N,i0 ,i,z, dimpari, dimdispari:Longint;
  3. V,pari, dispari, controllo: array of longint;
  4. n_ribaltamenti,numero_ribaltamenti, piccolidopo,maggioriprima,fuoriposto, ricordafuoriposto:int64;
  5. ordinabile: boolean;
  6.  
  7. function SOMMA(X:longint):int64;
  8. begin
  9. SOMMA:=0;
  10. while (X > 0) do
  11. begin
  12. X:=X-(X and -X);
  13. SOMMA:=SOMMA+controllo[X];
  14. end
  15. end;
  16. Procedure modifica(X:longint; D:longint);
  17. begin
  18. while (X <=D) do
  19. begin
  20. X:=X+(X and -X);
  21. controllo[X]:=controllo[X]+1;
  22. end
  23. end;
  24.  
  25. function paletta_sort(N: longint; V: array of longint): int64;
  26. begin
  27. if N mod 2 = 0 then begin dimpari:=N div 2; dimdispari:= dimpari; end
  28. else begin dimdispari:=N div 2 ; dimpari:= dimdispari+1; end;
  29. Setlength(pari, dimpari);
  30. Setlength(dispari, dimdispari);
  31. Setlength(controllo, N);
  32. ordinabile:=true;
  33. for z:=0 to N-1 do if V[z] mod 2 <> z mod 2 then begin ordinabile:=false; n_ribaltamenti:=-1; end;
  34. if ordinabile=true then begin
  35. for i:=1 to N do controllo[i]:=0;
  36. for i:=0 to N -1 do if i mod 2 =0 then pari[i div 2 +1 ]:=V[i] div 2 +1;
  37. for i:=0 to N -1 do if i mod 2 <> 0 then dispari[i div 2 +1 ]:=(V[i] -1) div 2 +1 ;
  38. for i:=1 to N div 2 do write (pari[i],' '); writeln;
  39. for i:=1 to N div 2 do write (dispari[i],' '); writeln;
  40. i:=1; n_ribaltamenti:=0;
  41. while i<=dimpari do
  42. begin
  43. piccolidopo:=0;
  44. modifica (i, dimpari);
  45. writeln (i,' ',controllo[i],' ');
  46. piccolidopo:=pari[i]-1 -somma(pari[i]);
  47. writeln(pari[i],' ',somma(pari[i]), ' ', piccolidopo);
  48. i:=i+1;
  49. end;
  50. n_ribaltamenti:=n_ribaltamenti+piccolidopo;
  51. for i:=1 to N do controllo[i]:=0;
  52. i:=1;
  53. while i<=dimdispari do
  54. begin
  55. piccolidopo:=0;
  56. modifica (i, dimdispari);
  57. piccolidopo:=dispari[i]-1-somma(dispari[i]);
  58. i:=i+1;
  59. end;
  60. n_ribaltamenti:=n_ribaltamenti+piccolidopo;
  61. end;
  62. paletta_sort:=n_ribaltamenti;
  63. end;
  64.  
  65.  
  66. begin
  67. (*assign(input, 'input.txt'); reset(input);
  68.   assign(output, 'output.txt'); rewrite(output);*)
  69. { Reading input }
  70. readln( N);
  71. Setlength(V, N);
  72. for i0 := 0 to N-1 do
  73. begin
  74. read(V[i0]);
  75. end;
  76. { Calling functions }
  77. numero_ribaltamenti := paletta_sort(N, V);
  78.  
  79. { Writing output }
  80. writeln(numero_ribaltamenti);
  81.  
  82. end.
  83.  
  84.  
  85.  
Success #stdin #stdout 0s 5308KB
stdin
6
0 1 2 3 4 5
stdout
1 2 3 
1 2 3 
1 0 
1 0 0
2 1 
2 0 1
3 0 
3 1 1
2