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