Program sairsodo;
const MAXN=100000;
type elenco = array[1..MaXN] of int64;
var N,i,d, idmediana, idmediana1, mediana:int64;
calcolacosto,calcolacosto1, altezzainiziale, altezzainiziale1:int64;
costo, costo1, costomin, ricordacostomin:int64;
H, ricordaltezze: elenco;
uscita, invariato:boolean;
ricordaintervallo:char;
Procedure scambia (var a,b: int64);
var x:int64;
begin
x:=a;
a:=b;
b:=x;
end;
Procedure ordinamento (estremoi,estremos:int64; var v : elenco; ordinato:boolean);
var inf, sup, medio:int64;
pivot :int64;
begin
inf:=estremoi;
sup:=estremos;
medio:= (estremoi+estremos) div 2;
pivot:=v[medio];
repeat
if (ordinato) then
begin
while (v[inf]<pivot) do inf:=inf+1;
while (v[sup]>pivot) do sup:=sup-1;
end;
if inf<=sup then
begin
scambia(v[inf],v[sup]);
inf:=inf+1;
sup:=sup-1;
end;
until inf>sup;
if (estremoi<sup) then ordinamento(estremoi,sup,v,ordinato);
if (inf<estremos) then ordinamento(inf,estremos,v,ordinato);
end;
begin
(* assign(input, 'input.txt'); reset(input);
assign(output, 'output.txt'); rewrite(output);*)
readln(N);
for i:=1 to N do begin read(H[i]); ricordaltezze[i]:=H[i]; end;readln;
costo:=0; costo1:=0; d:=0; ricordacostomin:=9223372036854775807; uscita:=false;
ordinamento (1,N,H, true);
invariato:=false; ricordaintervallo:=' ';
if N mod 2 <>0 then
begin
idmediana:=(N+1) div 2;
altezzainiziale:=H[idmediana]-(idmediana-1);
if altezzainiziale<0 then altezzainiziale:=0;
for i:=1 to N do
begin
calcolacosto:=(ricordaltezze[i] - (altezzainiziale +i-1));
if calcolacosto<0 then calcolacosto:=-calcolacosto;
costo:= costo + calcolacosto ;
end;
writeln(costo);
end
else
begin
idmediana:= N div 2;
idmediana1:=idmediana+1;
mediana:= (H[idmediana]+ H[idmediana + 1]) div 2; (*altezza che corrisponde alla mediana*)
while uscita=false do
begin
altezzainiziale:=mediana-d-(idmediana-1); (*vario di +-1 il valore della mediana finchè trovo il costo minore*)
altezzainiziale1:=mediana+d -(idmediana1-1); (*di conseguenza varia la altezza iniziale dalla quale partono i livelli*)
if altezzainiziale1<0 then begin d:=d+1; continue; end;
if altezzainiziale<0 then altezzainiziale:=0;
if altezzainiziale1<=0 then altezzainiziale1:=0;
for i:=1 to N do
begin
calcolacosto:=(ricordaltezze[i] - (altezzainiziale +i-1));
if calcolacosto<0 then calcolacosto:=-calcolacosto;
costo:= costo + calcolacosto ;
calcolacosto1:=(ricordaltezze[i] - (altezzainiziale1 +i-1));
if calcolacosto1<0 then calcolacosto1:=-calcolacosto1;
costo1:= costo1 + calcolacosto1 ;
end;
if costo<=costo1 then begin costomin:=costo; if ricordaintervallo='s' then invariato:=true; ricordaintervallo:='s'; end;
if costo1<costo then begin costomin:=costo1; if ricordaintervallo='d' then invariato:=true; ricordaintervallo:='d';end;
if (costomin<ricordacostomin) then ricordacostomin:=costomin
else if ((costomin=ricordacostomin) and (invariato=true )) then uscita:=true
else if (costomin>ricordacostomin) then uscita:=true;
writeln(mediana,' ',altezzainiziale,' ',altezzainiziale1, ' ',costo,' ',costo1,' ',costomin,' ',ricordaintervallo,' ',ricordacostomin, invariato);
costo:=0; costo1:=0; d:=d+1;
end;
writeln(ricordacostomin);
end;
end.
UHJvZ3JhbSBzYWlyc29kbzsKY29uc3QgTUFYTj0xMDAwMDA7IAp0eXBlIGVsZW5jbyA9IGFycmF5WzEuLk1hWE5dIG9mIGludDY0Owp2YXIgTixpLGQsIGlkbWVkaWFuYSwgaWRtZWRpYW5hMSwgbWVkaWFuYTppbnQ2NDsKICAgIGNhbGNvbGFjb3N0byxjYWxjb2xhY29zdG8xLCBhbHRlenphaW5pemlhbGUsIGFsdGV6emFpbml6aWFsZTE6aW50NjQ7CiAgICBjb3N0bywgY29zdG8xLCBjb3N0b21pbiwgcmljb3JkYWNvc3RvbWluOmludDY0OwogICAgSCwgcmljb3JkYWx0ZXp6ZTogZWxlbmNvOwogICAgdXNjaXRhLCBpbnZhcmlhdG86Ym9vbGVhbjsKIC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ICAgIGFzc2lnbihvdXRwdXQsICdvdXRwdXQudHh0Jyk7IHJld3JpdGUob3V0cHV0KTsqKQoJcmVhZGxuKE4pOwoJZm9yIGk6PTEgdG8gTiBkbyBiZWdpbiByZWFkKEhbaV0pOyByaWNvcmRhbHRlenplW2ldOj1IW2ldOyBlbmQ7cmVhZGxuOwoJY29zdG86PTA7IGNvc3RvMTo9MDsgZDo9MDsgcmljb3JkYWNvc3RvbWluOj05MjIzMzcyMDM2ODU0Nzc1ODA3OyB1c2NpdGE6PWZhbHNlOwoJb3JkaW5hbWVudG8gKDEsTixILCB0cnVlKTsgCglpbnZhcmlhdG86PWZhbHNlOyByaWNvcmRhaW50ZXJ2YWxsbzo9JyAnOwoJaWYgTiBtb2QgMiA8PjAgdGhlbiAKCSAgICAgICAgICAgICAgICAgYmVnaW4KCSAgICAgICAgICAgICAgICAgICBpZG1lZGlhbmE6PShOKzEpIGRpdiAyOwoJICAgICAgICAgICAgICAgICAgIGFsdGV6emFpbml6aWFsZTo9SFtpZG1lZGlhbmFdLShpZG1lZGlhbmEtMSk7CgkgICAgICAgICAgICAgICAgICAgaWYgYWx0ZXp6YWluaXppYWxlPDAgdGhlbiBhbHRlenphaW5pemlhbGU6PTA7CgkgICAgICAgICAgICAgICAgICAgZm9yIGk6PTEgdG8gTiBkbwoJICAgICAgICAgICAgICAgICAgICAgICAgIGJlZ2luIAoJICAgICAgICAgICAgICAgICAgICAgICAgICAgICBjYWxjb2xhY29zdG86PShyaWNvcmRhbHRlenplW2ldIC0gKGFsdGV6emFpbml6aWFsZSAraS0xKSk7CgkgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIGNhbGNvbGFjb3N0bzwwIHRoZW4gY2FsY29sYWNvc3RvOj0tY2FsY29sYWNvc3RvOwoJICAgICAgICAgICAgICAgICAgICAgICAgICAgICBjb3N0bzo9IGNvc3RvICsgY2FsY29sYWNvc3RvIDsKCSAgICAgICAgICAgICAgICAgICAgICAgICBlbmQ7CgkgICAgICAgICAgICAgICAgICAgICAgd3JpdGVsbihjb3N0byk7CgkgICAgICAgICAgICAgICAgZW5kCgkgICAgICAgICAgICAgIGVsc2UgCgkgICAgICAgICAgICAgICBiZWdpbgoJICAgICAgICAgICAgICAgIGlkbWVkaWFuYTo9IE4gZGl2IDI7CgkgICAgICAgICAgICAgICAgaWRtZWRpYW5hMTo9aWRtZWRpYW5hKzE7CgkgICAgICAgICAgICAgICAgbWVkaWFuYTo9IChIW2lkbWVkaWFuYV0rIEhbaWRtZWRpYW5hICsgMV0pIGRpdiAyOyAoKmFsdGV6emEgY2hlIGNvcnJpc3BvbmRlIGFsbGEgbWVkaWFuYSopCgkgICAgICAgICAgICAgICAgd2hpbGUgdXNjaXRhPWZhbHNlIGRvIAoJICAgICAgICAgICAgICAgICAgICAgIGJlZ2luCgkgICAgICAgICAgICAgICAgICAgICAJYWx0ZXp6YWluaXppYWxlOj1tZWRpYW5hLWQtKGlkbWVkaWFuYS0xKTsgKCp2YXJpbyBkaSArLTEgaWwgdmFsb3JlIGRlbGxhIG1lZGlhbmEgZmluY2jDqCB0cm92byBpbCBjb3N0byBtaW5vcmUqKQoJICAgICAgICAgICAgICAgICAgICAgICAgYWx0ZXp6YWluaXppYWxlMTo9bWVkaWFuYStkIC0oaWRtZWRpYW5hMS0xKTsgKCpkaSBjb25zZWd1ZW56YSB2YXJpYSBsYSBhbHRlenphIGluaXppYWxlIGRhbGxhIHF1YWxlIHBhcnRvbm8gaSBsaXZlbGxpKikKCSAgICAgICAgICAgICAgICAgICAgICAgIGlmIGFsdGV6emFpbml6aWFsZTE8MCB0aGVuIGJlZ2luIGQ6PWQrMTsgY29udGludWU7IGVuZDsKCSAgICAgICAgICAgICAgICAgICAgICAgIGlmIGFsdGV6emFpbml6aWFsZTwwIHRoZW4gYWx0ZXp6YWluaXppYWxlOj0wOwoJICAgICAgICAgICAgICAgICAgICAgICAgaWYgYWx0ZXp6YWluaXppYWxlMTw9MCB0aGVuIGFsdGV6emFpbml6aWFsZTE6PTA7CgkgICAgICAgICAgICAgICAgICAgICAgICBmb3IgaTo9MSB0byBOIGRvCgkgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGJlZ2luIAoJICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGNhbGNvbGFjb3N0bzo9KHJpY29yZGFsdGV6emVbaV0gLSAoYWx0ZXp6YWluaXppYWxlICtpLTEpKTsKCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiBjYWxjb2xhY29zdG88MCB0aGVuIGNhbGNvbGFjb3N0bzo9LWNhbGNvbGFjb3N0bzsKCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBjb3N0bzo9IGNvc3RvICsgY2FsY29sYWNvc3RvIDsKCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBjYWxjb2xhY29zdG8xOj0ocmljb3JkYWx0ZXp6ZVtpXSAtIChhbHRlenphaW5pemlhbGUxICtpLTEpKTsKCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiBjYWxjb2xhY29zdG8xPDAgdGhlbiBjYWxjb2xhY29zdG8xOj0tY2FsY29sYWNvc3RvMTsKCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBjb3N0bzE6PSBjb3N0bzEgKyBjYWxjb2xhY29zdG8xIDsKCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZW5kOwoJICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAoJICAgICAgICAgICAgICAgICAgICAgICAgaWYgY29zdG88PWNvc3RvMSB0aGVuIGJlZ2luIGNvc3RvbWluOj1jb3N0bzsgaWYgcmljb3JkYWludGVydmFsbG89J3MnIHRoZW4gaW52YXJpYXRvOj10cnVlOyByaWNvcmRhaW50ZXJ2YWxsbzo9J3MnOyBlbmQ7CgkgICAgICAgICAgICAgICAgICAgICAgICBpZiBjb3N0bzE8Y29zdG8gdGhlbiAgYmVnaW4gY29zdG9taW46PWNvc3RvMTsgaWYgcmljb3JkYWludGVydmFsbG89J2QnIHRoZW4gIGludmFyaWF0bzo9dHJ1ZTsgcmljb3JkYWludGVydmFsbG86PSdkJztlbmQ7CgkgICAgICAgICAgICAgICAgICAgICAgICAKCSAgICAgICAgICAgICAgICAgICAgICAgIGlmIChjb3N0b21pbjxyaWNvcmRhY29zdG9taW4pIHRoZW4gcmljb3JkYWNvc3RvbWluOj1jb3N0b21pbgoJICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGVsc2UgaWYgKChjb3N0b21pbj1yaWNvcmRhY29zdG9taW4pIGFuZCAoaW52YXJpYXRvPXRydWUgKSkgIHRoZW4gdXNjaXRhOj10cnVlCgkgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZWxzZSBpZiAoY29zdG9taW4+cmljb3JkYWNvc3RvbWluKSAgdGhlbiB1c2NpdGE6PXRydWU7CgkgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCgkgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKCSAgICAgICAgICAgICAgICAgICAgICAgIHdyaXRlbG4obWVkaWFuYSwnICcsYWx0ZXp6YWluaXppYWxlLCcgJyxhbHRlenphaW5pemlhbGUxLCAnICcsY29zdG8sJyAnLGNvc3RvMSwnICcsY29zdG9taW4sJyAnLHJpY29yZGFpbnRlcnZhbGxvLCcgJyxyaWNvcmRhY29zdG9taW4sIGludmFyaWF0byk7CgkgICAgICAgICAgICAgICAgICAgICAgICBjb3N0bzo9MDsgY29zdG8xOj0wOyBkOj1kKzE7CgkgICAgICAgICAgICAgICAgICAgICBlbmQ7IAoJICAgICB3cml0ZWxuKHJpY29yZGFjb3N0b21pbik7ICAgICAgICAgIAoJICBlbmQ7IAplbmQu