这个可以先排好序,然后计算出的后面的点的关系与前面的点的已经没有关系,既然有关系前面的点已经计算过。然后排好序(这里不会费多少时间的)找出有多少共线即可。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<set>
#include<map>
using namespace std;
#define N 1001
struct my
{
int x,y;
bool operator<(my a)
{
if (a.x==x)
return a.y<y;
else
return a.x<x;
}
}go[N];
double cc(my a,my b)
{
if (a.x==b.x)
return 100000000;
return (double)(b.y-a.y)/(double)(b.x-a.x);
}
int n;
double cur[N*N];
int main()
{
int i,j,k;
while (cin>>n)
{
for (i=0;i<n;i++)
scanf("%d%d",&go[i].x,&go[i].y);
sort(go,go+n);
int l;
int ans=2;
for (i=0;i<n-1;i++)
{
l=0;
for (j=i+1;j<n;j++)
{
double temp=cc(go[i],go[j]);
cur[l++]=temp;
}
sort(cur,cur+l);
int num=0;
//cout<<l<<endl;
for (j=1;j<l;j++)
{
num=2;
//cout<<cur[j-1]<<' '<<cur[j]<<' '<<cur[j-1]-cur[j]<<endl;
while (j<l&&fabs(cur[j-1]-cur[j])<=1e-6)
num++,j++;
ans=max(ans,num);
}
}
cout<<ans<<endl;
}
return 0;
}
Linearity
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 6965 | Accepted: 1578 |
Description
Alice often examines star maps where stars are represented by points in a plane and there is a Cartesian coordinate for each star. Let the Linearity of a star map be the maximum number of stars in a straight line.
For example, look at the star map shown on the figure above, the Linearity of this map is 3, because the star 1, star 2, and star 5 are within the same straight line, and there is no straight line that passes 4 stars.
You are to write a program to find the Linearity of a star map.
For example, look at the star map shown on the figure above, the Linearity of this map is 3, because the star 1, star 2, and star 5 are within the same straight line, and there is no straight line that passes 4 stars.
You are to write a program to find the Linearity of a star map.
Input
Input will contain multiple test cases. Each describes a star map.
For each test case, the first line of the input contains the number of stars N (2 <= N <= 1000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0 <= X, Y <= 1000). There can be only one star at one point of the plane.
For each test case, the first line of the input contains the number of stars N (2 <= N <= 1000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0 <= X, Y <= 1000). There can be only one star at one point of the plane.
Output
Output the Linearity of the map in a single line.
Sample Input
5 0 0 2 0 0 2 1 1 2 2
Sample Output
3
Source