Codeforces Round #308 (Div. 2) —— D (思维题)

D. Vanya and Triangles
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Vanya got bored and he painted n distinct points on the plane. After that he connected all the points pairwise and saw that as a result many triangles were formed with vertices in the painted points. He asks you to count the number of the formed triangles with the non-zero area.

Input

The first line contains integer n (1 ≤ n ≤ 2000) — the number of the points painted on the plane.

Next n lines contain two integers each xi, yi ( - 100 ≤ xi, yi ≤ 100) — the coordinates of the i-th point. It is guaranteed that no two given points coincide.

Output

In the first line print an integer — the number of triangles with the non-zero area among the painted points.

Sample test(s)
input
4
0 0
1 1
2 0
2 2
output
3
input
3
0 0
1 1
2 0
output
1
input
1
1 1
output
0
Note

Note to the first sample test. There are 3 triangles formed: (0, 0) - (1, 1) - (2, 0)(0, 0) - (2, 2) - (2, 0)(1, 1) - (2, 2) - (2, 0).

Note to the second sample test. There is 1 triangle formed: (0, 0) - (1, 1) - (2, 0).

Note to the third sample test. A single point doesn't form a single triangle.


题意:给你n个点的坐标,把所有两点都相连,求共能组成多少个三角形。

一开始居然还在想搜索= =

方法1:

如果没有x(x > 2)个点连成一线的情况,那么答案就是C(n,3)。如果有x(x > 2)个点连成一线的情况,那么这些点就是导致组成的三角形的个数少于C(n,3)的原因(其它点是不会造成影响的)。所以只要减去这些点所影响三角形的个数就是答案。而这些连成一线点不会组成三角形,但本来可以组成C(x,3)的,所以就减少了C(x,3)个。由于计算方便,可以枚举一个点的同时枚举另一个点,记录下这两个点所组成的斜率,然后排序找出有cnt个斜率相等。这cnt个斜率相等表示枚举那个点时有cnt+1个点连成一线,那么减少的三角形个数为C(cnt,2)。时间复杂度为O(n*n*logn)


方法2:

看到别人写的,居然可以这样==,我怎么没想到==#

直接暴力枚举三点,然后计算其面积,如果不为0则能组成三角形,否则不能。时间复杂度为O(n^3)。似乎在cf上10^9的复杂度1sec都可以过。

你可能感兴趣的:(三角形,CF,思维题)