【问题描述】
对于40%的数据,N ≤1000;
对于100%的数据,N≤300000。
平面上有N条直线,用方程Aix + Biy +Ci =0表示。这些直线没有三线共点的。现在要你计算出用这些直线可以构造出多少三角形?
输入:
第1行:一个整数N(1 ≤ N≤ 300000)。
下面N行:每行3个整数:Ai, Bi 和Ci,表示对应直线方程的系数。不超过10^9.
input 1 6 0 1 0 -5 3 0 -5 -2 25 0 1 -3 0 1 -2 -4 -5 29
| input 2 5 -5 3 0 -5 -3 -30 0 1 0 3 7 35 1 -2 -1
|
output 1 10
| output 2 10
|
对于40%的数据,N ≤1000;
对于100%的数据,N≤300000。
分析:
显然任何不平行的三条直线一定能构成一个三角形(交于一点除外),所以可以三重循环枚举三条直线的组合。但是不够优,我们可以先将直线按斜率排序,显然斜率不相等的两条直线一定相交,那么我们假设枚举的三条直线k1<k2<k3,那么我们可以枚举k2,然后计算k1,和k3的个数,乘法原理即可,因为已经排序好了,k1和k3的数量的维护也就不麻烦了。
参考程序:
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=510000;
typedef long long LL;
int n;
struct Point{
LL x,y;
bool operator < (const Point k)const{
return x*k.y<y*k.x;
}
Point(LL x,LL y):x(x),y(y){}
Point(){}
}p[maxn];
int main(){
freopen("trokuti.in","r",stdin);
freopen("trokuti.out","w",stdout);
scanf("%d",&n);
for (int i=1;i<=n;i++){
LL a,b,c;
scanf("%lld%lld%lld",&a,&b,&c);
p[i]=Point(a,b);
}
sort(p+1,p+n+1);
int l=1,r=1;
LL res=0;
for (int i=1;i<=n;i++){
while (p[l]<p[i])l++;
while (r<=n && !(p[i]<p[r]))r++;
res+=(LL)(l-1)*(n+1-r);
}
printf("%lld",res);
return 0;
}