题目链接:https://codeforces.com/contest/1163/problem/C2
题目大意:给出n个点(点不重复)。每个点之间都有一条线相连。重合的直线算一条。问有多少对直线相交。
思路:一个常见的方法,map种存上斜率。然后判断斜率不相等的直线的个数。这道题因为重合的直线算一条。所以我又开了一个一般式的直线去重。最后n*n枚举直线,log(n)查询即可。
个人感觉多了几个log,时间花费似乎有点大。但还没想到更好的思路优化。
ACCode:
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<time.h>
#include<math.h>
// srand((unsigned)time(NULL));rand();
#include<map>//unordered_map
#include<set>//multiset
#include<deque>
#include<queue>
#include<stack>
#include<bitset>
#include<string>
#include<fstream>
#include<iostream>
#include<algorithm>
#define ll long long
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define clean(a,b) memset(a,b,sizeof(a))
using namespace std;
const int MAXN=1e3+10;
//const int MAXM=10;
const int INF32=0x3f3f3f3f;
const ll INF64=0x3f3f3f3f3f3f3f3f;
const ll MOD=1e9+7;
const double PI=acos(-1.0);
const double EPS=1.0e-8;
//unsigned register
// ios::sync_with_stdio(false)
struct Line{
int A,B,C;
void Intt(){
int tmp=__gcd(A,__gcd(B,C));
A/=tmp;B/=tmp;C/=tmp;
if(A<0||(A==0&&B<0)) A=-A,B=-B,C=-C;
}
friend int operator < (Line a,Line b){
if(a.A!=b.A) return a.A<b.A;
if(a.B!=b.B) return a.B<b.B;
return a.C<b.C;
}
};
struct Point{
ll x,y;
Point(ll _x=0,ll _y=0){
x=_x;y=_y;
}
void Intt(){ if(x<0||(x==0&&y<0)) x=-x,y=-y; }
friend Point operator - (Point a,Point b){
return Point(a.x-b.x,a.y-b.y);
}
friend ll operator ^ (Point a,Point b){
return a.x*b.y-a.y*b.x;
}
friend int operator < (Point a,Point b){
a.Intt();b.Intt();
return (a^b)<0;
}
};
map<Line,int> CntL;//重合的
map<Point,int> CntP;//平行&&重合的
Point Dots[MAXN];
int n;
int main(){
while(~scanf("%d",&n)){
CntL.clear();CntP.clear();
for(int i=1;i<=n;++i){
scanf("%lld%lld",&Dots[i].x,&Dots[i].y);
}
int tot=0;
for(int i=1;i<=n;++i){
for(int j=i+1;j<=n;++j){
Point tmp=Dots[i]-Dots[j];tmp.Intt();
Line l;l.A=Dots[j].y-Dots[i].y;l.B=Dots[i].x-Dots[j].x;l.C=-(Dots[i]^Dots[j]);l.Intt();
CntL[l]++;
if(CntL[l]==1) CntP[tmp]++,tot++;
}
}
ll ans=0;
for(int i=1;i<=n;++i){
for(int j=i+1;j<=n;++j){
Point tmp=Dots[i]-Dots[j];tmp.Intt();
Line l;l.A=Dots[j].y-Dots[i].y;l.B=Dots[i].x-Dots[j].x;l.C=-(Dots[i]^Dots[j]);l.Intt();
int samecntl=CntL[l],samecntp=CntP[tmp];//重合的,平行||重合的
// printf("ans=%lld samecntp=%d samecntl=%d tot=%d\n",ans,samecntp,samecntl,tot);
if(samecntl==0) continue ;
ans+=tot-samecntp;
CntL[l]=0;CntP[tmp]-=1;tot-=1;//重合的都取消掉
}
}printf("%lld\n",ans);
}
}