Cod sursa(job #1213603)

Utilizator RusuAlexeiRusu Alexei RusuAlexei Data 28 iulie 2014 16:39:27
Problema Numarare triunghiuri Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.33 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
         i:=l;
         j:=r;
         x:=a[(l+r) div 2];
         repeat
           while a[i]<x do
            inc(i);
           while x<a[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=a[i];
                a[i]:=a[j];
                a[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j 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.