hdu-5020-Revenge of Collinearity

来源:互联网 发布:淘宝最新版本下载2016 编辑:程序博客网 时间:2024/06/06 12:42
Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 262    Accepted Submission(s): 72




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
 


Source

BestCoder Round #10


题目大意:给你n个点的坐标  每个点都是整数  问你三个点共线的情况有多少种。。其中如果a,b,c共线的话 (a,b,c) , (c,b,a)算同一种

思路:把n个点先按x从小到大,x相同 按y从小到大排。从第一个点往后面的点扫。。把改点与后面所有点的斜率保存起来,然后把斜率排好序  o(n)扫一遍。。斜率刚好两个相同的,说明 刚好三点共线。。ans++,如果相同的个数sum大于2,ans+= 从sun中选两个(既sum*(sun-1)/2),因为此时有sum + 1 个点共线。。其中第一个点必选。。然后剩下的任选两个。。



代码:

#include <cstdio>#include <cmath>#include <cstdlib>#include <cstring>#include <iostream>#include <queue>#include <algorithm>typedef __int64 LL ;using namespace std;double a[1100];double e = 0.00000000004;struct point{    double x,y;}p[1100];bool cmp(point a, point b){    if(a.x != b.x) return a.x < b.x;    else return a.y < b.y;}int main(){    int n,t,x[1100],y[1100],i,j;    cin>>t;    while (t--)    {        scanf("%d",&n);        for(i = 0; i < n; i++)        {            scanf("%lf %lf",&p[i].x, &p[i].y);        }        sort(p,p + n, cmp);        LL ans = 0;        for(i = 0; i < n; i++)        {            double x1 =  p[i].x;            double y1 =  p[i].y;            memset(a,0,sizeof(a));            int vv = 0;            for(j = i + 1; j < n; j++)            {                double x2 =  p[j].x;                double y2 =  p[j].y;                if(x1 - x2 == 0)                {                    a[vv++] = 1000000000 + 5;                }                else                {                    a[vv++] = (x1 - x2)/(y1 - y2);                }            }            sort(a,a + vv);            double k = a[0];            int sum = 1;            for(j = 1; j < vv; j++)            {                if(a[j] == k)   // 这里太奇葩了,居然写成fabs(a[j] - k) < e居然过不了                {                    sum++;                }                else                {                    if(sum == 2)                        ans++;                    else if(sum > 2)                    {                        ans+= (sum * (sum-1)) / 2;                    }                    sum = 1;                    k = a[j];                }            }            if(sum == 2)                ans++;            if(sum > 2)            {                 ans+= (sum * (sum-1)) / 2;            }        }        cout<<ans<<endl;    }    return 0;}


0 0
原创粉丝点击