题目大意:
平面上有 n n 个点,求出用这些点可以构成的三角形数。
,坐标的绝对值不超过 104 10 4 ,保证没有重合的点。
分析:
N
N
个点中选个点构成图形的方案为
C3n
C
n
3
枚举第一个点,对于剩下的点按照和这个点连线的斜率排序一下,把相同斜率的线段放在一起,可以算出选到两条相同斜率的线段方案数,即不能构成三角形的方案数,然后减去,注意处理斜率不存在的情况。
代码:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define N 3005
#define eps 1e-9
using namespace std;
typedef long long ll;
struct Node {int x, y; }a[N];
double c[N];
ll n, ans;
int cnt;
bool cmp(Node aa, Node bb)
{
if (aa.y == bb.y) return aa.x > bb.x;
return aa.y > bb.y;
}
int main()
{
scanf("%lld", &n);
if (n == 1 || n == 2)
{
printf("0\n");
return 0;
}
ans = n * (n - 1) * (n - 2) / 6;
for (int i = 1; i <= n; i++)
scanf("%d %d", &a[i].x, &a[i].y);
sort(a + 1, a + n + 1, cmp);
for (int i = 1; i < n; i++)
{
cnt = 0;
for (int j = i + 1; j <= n; j++)
{
if (a[j].x == a[i].x) c[++cnt] = -10005;
else if (a[j].y == a[i].y) c[++cnt] = -10006;
else c[++cnt] = (double)(a[i].y - a[j].y) / (a[i].x - a[j].x);
}
sort(c + 1, c + cnt + 1);
ll tot = 1;
c[cnt + 1] = 10005;
for (int i = 2; i <= cnt + 1; i++)
if (c[i] <= c[i - 1] + eps) tot++;
else
{
if (tot != 1) ans = ans - (tot) * (tot - 1) / 2;
tot = 1;
}
}
printf("%lld\n", ans);
return 0;
}