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都可以过。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值