Cod sursa(job #120964)

Utilizator CezarMocanCezar Mocan CezarMocan Data 7 ianuarie 2008 14:13:52
Problema Medie Scor 60
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.23 kb
var v:array[1..9000]of longint;
    x,y:array[1..7000]of longint;
    i,j,n,s,t1,t2,aux:longint;

procedure qsort(ls,ld:longint);
var i,j,aux:longint;
begin
  i:=ls;j:=ld;
  while true do begin
    while (v[i]<=v[j])and(i<>j) do inc(i);
    if i=j then break;
    aux:=v[i];v[i]:=v[j];v[j]:=aux;dec(j);
    while (v[i]<=v[j])and(i<>j) do dec(j);
    if i=j then break;
    aux:=v[i];v[i]:=v[j];v[j]:=aux;inc(i);
  end;
  if j-1>ls then qsort(ls,j-1);
  if j+1<ld then qsort(j+1,ld);
end;


begin
assign(input,'medie.in');reset(input);
assign(output,'medie.out');rewrite(output);
readln(n);
for i:=1 to n do
        begin
        read(v[i]);
        x[v[i]]:=x[v[i]]+1;
        end;
randomize;
for i:=1 to n do
        begin
        t1:=random(n)+1;
        t2:=random(n)+1;
        aux:=v[t1];
        v[t1]:=v[t2];
        v[t2]:=aux;
        end;
qsort(1,n);
for i:=1 to n-1 do
        for j:=i+1 to n do
                begin
                if ((v[i]+v[j]) shr 1)shl 1=v[i]+v[j] then
                        y[(v[i]+v[j]) shr 1]:=y[(v[i]+v[j]) shr 1]+1;
                end;
for i:=1 to n do
        if y[v[i]]<>0 then
                s:=s+y[v[i]]-x[v[i]]+1;
writeln(s);
close(input);close(output);
end.