PRAVO - Tam giác vuông

Tác giả: khuc_tuan

Ngôn ngữ: Pascal

const
Epsilon = 1E-12;
Max = 1500;
var
goc, x, y: array[1..Max] of extended;
Left, Right:array[1..Max] of extended;
key, result: extended;
m, n, c: longint;
procedure Read_Data;
var
i: longint;
begin
Read(n);
For i := 1 to n do
Read(x[i], y[i]);
end;
procedure Swap(i, j: longint);
var
temp: extended;
begin
temp := goc[i];
goc[i] := goc[j];
goc[j] := temp;
end;
procedure Sort(L, R: longint);
var
i, j: longint;
k: extended;
begin
If L >= R then Exit;
i := L;
j := R;
k := goc[(i+j) shr 1];
Repeat
While goc[i] < k do inc(i);
While goc[j] > k do dec(j);
if i <= j then
begin
If i < j then Swap(i, j);
inc(i);
dec(j);
end;
Until i > j;
Sort(L, j);
Sort(i, R);
end;
function theta(x1, y1, x2, y2: extended): extended;
var
t, dx, dy, ax, ay: extended;
begin
dx := x2 - x1; ax := abs(dx);
dy := y2 - y1; ay := abs(dy);
If (dx = 0) and (dy = 0) then t := 0 else t := dy/(ax + ay);
If dx < 0 then t := 2 - t
else
if dy < 0 then t := 4 + t;
theta := t * 90;
end;
procedure Calculate;
var
i: longint;
begin
For i := 2 to m do
If abs(Goc[i] - Goc[i-1]) < Epsilon then
Left[i] := Left[i-1];
For i := m-1 downto 1 do
If abs(Goc[i] - Goc[i+1]) < Epsilon then
Right[i] := Right[i+1];
end;
procedure Init(i: longint);
var
j: longint;
begin
m := 0;
For j := 1 to n do
If j <> i then
begin
m := m + 1;
Left[m] := m;
Right[m] := m;
Goc[m] := theta(x[i], y[i], x[j], y[j]);
end;
Sort(1, m);
Calculate;
end;
procedure Find_Key(i: longint);
begin
If abs(Goc[i] - 270) < Epsilon then
begin
key := 0;
Exit;
end;
If Goc[i] < 270 then
key := Goc[i] + 90
Else
key := Goc[i] - 270;
end;
procedure Binary_search;
var
L, R, mid: longint;
begin
c := 0;
L := 1;
R := m;
Repeat
mid := (L + R) shr 1;
If abs(Goc[mid] - key) < Epsilon then
begin
c := mid;
Break;
end
else
If Goc[mid] < key then L := mid + 1
else R := mid - 1;
Until L > R;
end;
procedure Solve;
var
i, j: longint;
begin
result := 0;
For i := 1 to n do
begin
Init(i);
For j := 1 to m do
begin
Find_key(j);
Binary_search;
If c > 0 then result := result + Right[c] - Left[c] + 1;
end;
end;
end;
procedure Write_answer;
begin
Writeln( result :0: 0);
end;
begin
Read_Data;
Solve;
Write_answer;
end.

Download