Cod sursa(job #1213610)

Utilizator RusuAlexeiRusu Alexei RusuAlexei Data 28 iulie 2014 16:52:54
Problema Numarare triunghiuri Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.18 kb
program betisoare;
  var n,i,j,l,r,ans:longint;
      a:array[1..800] of longint;

procedure sort(l,r:longint);
  var i,j,x,y:longint;
  begin
    x:=a[(l+r)div 2];
    i:=l;j:=r;
    repeat
        while a[i]<x do inc(i);
        while a[j]>x do dec(j);
        if i<=j then
          begin
            y:=a[i];
            a[i]:=a[j];
            a[j]:=y;
            inc(i);
            dec(j);
          end;
    until i>j;
    if j>l then sort(l,j);
    if i<r then sort(i,r);
  end;



function cbin2(l,r,val:longint):longint;
  var m:longint;
  begin
    if l=r then cbin2:=l else
      begin
        m:=(l+r)div 2;
        if a[m+1]<=val then cbin2:=cbin2(m+1,r,val)
                       else cbin2:=cbin2(l,m,val);
      end;

  end;

begin
  assign(input,'nrtri.in');
  reset(input);
  assign(output,'nrtri.out');
  rewrite(output);

  readln(n);
  for i:=1 to n do read(a[i]);

  sort(1,n);


  for i:=1 to n-2 do
    for j:=i+1 to n-1 do
        begin
          if a[n]<=a[i]+a[j] then r:=n else r:=cbin2(1,n,a[i]+a[j]);
          l:=j+1;
          if l<=r then ans:=ans+r+1-l;
        end;
  writeln(ans);
  close(output);
end.