您的位置:首页 > 其它

51nod 1451 合法三角形

2017-10-24 16:29 218 查看

题目

有n个不同的点,问有多少组三元组能构成面积非0的三角形。

Input

单组测试数据。

第一行一个整数n (1 ≤ n ≤ 2000),表示点的数目。

接下来n行,每行包含两个整数 xi, yi ( -100 ≤ xi, yi ≤ 100),表示第i个点的坐标。输入保证点是两两不同的。

Output

输出合法的三角形数目。

Input示例

4

0 0

1 1

2 0

2 2

Output示例

3

分析

n^2连边计算斜率然后减去斜率相同的就好

代码

#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

typedef long long ll;

const int MAXN = 2222;
const int INF = 0x3f3f3f3f;
const double ESP = 1e-6;

ll n;
ll rep = 0;
ll x[MAXN], y[MAXN];
double slope[MAXN * MAXN];

void solve()
{
for (int i = 1; i <= n; i++)
{
int k = 0;
for (int j = i + 1; j <= n; j++)
{
double x_ = x[j] - x[i];
double y_ = y[j] - y[i];

double res;
if (x_ == 0)
{
res = INF;
}
else
{
res = y_ / x_;
}
slope[k++] = res;
}

sort(slope, slope + k);

int last = 0, now = 1, tmp;
for (; now < k; now++)
{
if (abs(slope[now] - slope[last]) > ESP)
{
tmp = now - last;
rep += tmp * (tmp - 1) / 2;
last = now;
}
}
tmp = now - last;
rep += tmp * (tmp - 1) / 2;
}
}

int main()
{
scanf("%lld", &n);

for (int i = 1; i <= n; i++)
{
scanf("%lld%lld", &x[i], &y[i]);
}

solve();

printf("%lld\n", n * (n - 1) * (n - 2) / 6 - rep);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: