HDU5020(判断三点共线)BestCoder Round #10(1003)

本文主要探讨了在BestCoder Round #10(1003)比赛中遇到的HDU5020题目,该题涉及到几何中的三点共线判断问题。通过对几何原理的深入分析,文章揭示了解决此类问题的关键思路和算法实现。

Revenge of Collinearity

Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 184    Accepted Submission(s): 51


Problem Description
In geometry, collinearity is a property of a set of points, specifically, the property of lying on a single line. A set of points with this property is said to be collinear (often misspelled as colinear).
---Wikipedia

Today, Collinearity takes revenge on you. Given a set of N points in two-dimensional coordinate system, you have to find how many set of <Pi, Pj, Pk> from these N points are collinear. Note that <Pi, Pj, Pk> cannot contains same point, and <Pi, Pj, Pk> and <Pi, Pk, Pj> are considered as the same set, i.e. the order in the set doesn’t matter.
 

Input
The first line contains a single integer T, indicating the number of test cases. 

Each test case begins with an integer N, following N lines, each line contains two integers Xi and Yi, describing a point.

[Technical Specification]
1. 1 <= T <= 33
2. 3 <= N <= 1 000
3. -1 000 000 000 <= Xi, Yi <= 1 000 000 000, and no two points are identical.
4. The ratio of test cases with N > 100 is less than 25%.
 

Output
For each query, output the number of three points set which are collinear.
 

Sample Input
2 3 1 1 2 2 3 3 4 0 0 1 0 0 1 1 1
 

Sample Output
1 0

题意:给出n个点,找出三点贡献的点的集合

思路:n=1000,我比赛的时候实在没思路,虽然知道暴力n^3一定会超时,但还也写完意思的交了一发,平时几乎不碰几何党的飘过~

            赛后看了某神犇的代码,思路瞬间开阔了

            就是将n个点按x,y从小到大排序,x为第一关键字,y为第二关键字

            排完序以后就是两层for暴搜了,因为已经排好序,就可以按斜率将所有斜率一样的点归为一个集合

            假设当前要判断的点为i,有p个点与之共线,因为i是一定要选的,所以只需在剩下的p个点中选择2个就好了,为p*(p-1)/2

            累加这些就是答案了

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

struct node{
    int x,y;
}vex[1010];

typedef pair pii;
map< pii , int >mp;
map< pii , int >::iterator it;

int gcd(int a,int b)
{
    if(a==0)return b;
    if(b==0)return a;
    if(a>b)swap(a,b);
    while(b)
    {
        int temp=a%b;
        a=b;
        b=temp;
    }
    return a;
}

bool cmp(node a,node b)
{
    if(a.x==b.x)return a.ysecond >=2 ){
                    int x=it->second;
                    ans+=x*(x-1)/2;
                }
            }
        }

        printf("%d\n",ans);

    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值