codeforces#1163C2. Power Transmission (Hard Edition)(计算几何)

题目链接:

https://codeforces.com/contest/1163/problem/C2  

题意:

给出$n$个点,任意两点连接一条直线,求相交直线的对数

数据范围:

$1 \le n \le 10^3$

分析: 

先建立所有的直线,可以把直线定义成$ax+ by=c$,但是需要把$a$和$b$的大小化简成最小,保证直线的唯一性

$k=\frac{y_{1}-y_{2}}{x_{1}-x_{2}}$,上下化简,再用点斜式构造$ax+ by=c$

ac代码:

#include<bits/stdc++.h>
#define  ll long long
#define pa pair<int,int>
using namespace std;
const int maxn=1000+10;
struct Point
{
    int x,y;
}point[maxn];
map< pa,set<int> >ma;
int n;
int main ()
{
    int cnt=0;
    ll ans=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d %d",&point[i].x,&point[i].y);
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            int x1=point[i].x,y1=point[i].y;
            int x2=point[j].x,y2=point[j].y;
            int a=(y1-y2),b=(x2-x1),c=(x2-x1)*y1+x1*(y1-y2);
            int f=__gcd(a,b);
            a/=f,b/=f,c/=f;
            if(a<0||(a==0&&b<0))a=-a,b=-b,c=-c;
            pa now=make_pair(a,b);
            if(ma[now].find(c)==ma[now].end())
            {
                cnt++;
                ma[now].insert(c);
                ans+=cnt-(int)ma[now].size();
            }
        }
    }
    printf("%lld\n",ans);
    return 0;
}

  

 

posted @ 2019-05-15 19:43  czh~  阅读(268)  评论(0编辑  收藏  举报