Cod sursa(job #96532)

Utilizator juniorOvidiu Rosca junior Data 2 noiembrie 2007 06:28:47
Problema Numarare triunghiuri Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.18 kb
var
  fi, fo : text;
  i, j, n, aux, nt, l12, k, s, d, m : longint;
  a : array[1..802] of longint;
begin
	assign(fi,'nrtri.in'); reset(fi);
	assign(fo,'nrtri.out'); rewrite(fo);
  readln(fi,n);
  for i := 1 to n do
  	read(fi,a[i]);
  for i := 1 to n-1 do
  	for j := i+1 to n do
    	if a[i] > a[j] then
      	begin
      		aux := a[i]; a[i] := a[j]; a[j] := aux;
        end;
  //a[n+1] := 61000; a[n+2] := 61000;
  for i := 1 to n-2 do
  	begin write(nt, ' ');
  	for j := i+1 to n-1 do
    	begin
      	l12 := a[i]+a[j]; // suma lungimilor primelor 2 segmente
      	s := j; d := n;   // Cautam cel mai mare indice pentru care l12 >= a[m].
        repeat
        	m := (s+d) div 2;
          if l12 >= a[m] then
          	s := m
          else
          	d := m
        until d-s <= 1;
        if l12 < a[m] then // Valoarea corecta poate fi m-1, m, m+1 sau m+2.
        	dec(m)
        else
        	begin
            if (m+1 <= n) and (l12 >= a[m+1]) then
            	inc(m);
            if (m+2 <= n) and (l12 >= a[m+2]) then
            	inc(m);
          end;
        inc(nt,m-j);
      end;
    end;
  write(fo,nt);
  close(fi); close(fo);	
end.