题目大意
给出二维平面上的
n
n
n个点,这些点两两相连,构成了一些直线,其中重复的直线算一条(比如:三点共线只算是一条直线而不是三条)。
问这些这些有多少对是相交的。
时间限制
3s
数据范围
n ≤ 1 0 4 n\le 10^4 n≤104
题解
正难则反,计算不相交的直线,即平行的直线。
这个非常好判断,只需要斜率相等即可。
现在有一个问题,如果找出所有的直线,
不妨先枚举一个点
i
i
i,然后再再
[
1
,
i
)
[1,i)
[1,i)中枚举一个点,此时两点就能确立一条直线了。
现在判断一下这些直线的斜率是否相同,如果出现相同,则这两条或者多条直线都是重复的。
Code
//#pragma GCC optimize (2)
//#pragma G++ optimize (2)
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#define ll long long
#define G getchar
using namespace std;
int read()
{
char ch;
for(ch = G();(ch < '0' || ch > '9') && ch != '-';ch = G());
int n = 0 , w;
if (ch == '-')
{
w = -1;
ch = G();
} else w = 1;
for(;'0' <= ch && ch <= '9';ch = G())n = (n<<1)+(n<<3)+ch-48;
return n * w;
}
void write(ll x)
{
if (x > 9)
{
write(x / 10);
putchar(x % 10 + 48);
}
else putchar(x + 48);
}
const int N = 1003;
int n , m;
long long ans , tmp , cnt;
int x[N] , y[N];
double b[N] , a[N * N];
double calc(int i , int j)
{
if (x[i] == x[j]) return 2147483647;
return 1.0 * (y[i] - y[j]) / (x[i] - x[j]);
}
int main()
{
//freopen("i.in","r",stdin);
//freopen("e.out","w",stdout);
n = read();
m = 0;
for (int i = 1 ; i <= n; i++)
{
x[i] = read();
y[i] = read();
for (int j = 1 ; j < i ; j++)
b[j] = calc(j , i);
sort(b + 1 , b + i);
b[0] = b[1] - 1;
b[i] = b[i - 1] + 1;
for (int j = 1 ; j < i ; j++)
{
if (b[j] == b[j - 1] || b[j] == b[j + 1]) continue;
cnt++;
a[cnt] = b[j];
}
}
m = cnt;
sort(a + 1 , a + 1 + m);
a[0] = a[1] - 1;
a[m + 1] = a[m] + 1;
tmp = 0;
ans = cnt * (cnt - 1) / 2;
for (int i = 1 ; i <= m + 1 ; i++)
{
if (a[i] != a[i - 1])
{
cnt++;
ans = ans - tmp * (tmp - 1) / 2;
tmp = 0;
}
tmp++;
}
printf("%lld\n", ans);
return 0;
}