Lining Up
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1274 Accepted Submission(s): 366
Problem Description
``How am I ever going to solve this problem?" said the pilot.
Indeed, the pilot was not facing an easy task. She had to drop packages at specific points scattered in a dangerous area. Furthermore, the pilot could only fly over the area once in a straight line, and she had to fly over as many points as possible. All points were given by means of integer coordinates in a two-dimensional space. The pilot wanted to know the largest number of points from the given set that all lie on one line. Can you write a program that calculates this number?
Your program has to be efficient!
Input
The input consists of multiple test cases, and each case begins with a single positive integer on a line by itself indicating the number of points, followed by N pairs of integers, where 1 < N < 700. Each pair of integers is separated by one blank and ended by a new-line character. No pair will occur twice in one test case.
Output
For each test case, the output consists of one integer representing the largest number of points that all lie on one line, one line per case.
Sample Input
5
1 1
2 2
3 3
9 10
10 11
Sample Output
3
题解:错了好一会儿,发现是排序那里写错了,多此一举。。。都怪以前的qsort,使我现在都快不敢直接判断了。。。
思路是先找出所有点,求出相同直线的个数sum,根据n*(n - 1)/2=sum,求出n;借助队友的思路;
ac代码:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; typedef long long LL; int tp; struct Point{ double x, y; Point(){ } Point(double x, double y){ this->x = x; this->y = y; } }; Point point[1010]; struct Node{ double k, b; Node(double k,double b){ this->k = k; this->b = b; } Node(){ } bool operator < (const Node &a) const{ if(k != a.k){//直接比就可以。。。 return k < a.k; } else// return b < a.b; } }; Node dt[250000]; Node operator + (Point a,Point b){ double k, t; k = (a.y - b.y) / (a.x - b.x); t = a.y - k * a.x; return Node(k,t); } bool operator == (Node a, Node b){ if(abs(a.k - b.k) < 1e-6){ if(abs(a.b - b.b) < 1e-6){ return true; } } return false; } int getn(int a, int b, int c){ double t = b * b - 4 * a * c; double x = ( -b + sqrt(t) ) / (2.0 * a); return (int)x; } int main(){ int N; while(~scanf("%d",&N)){ double x, y; tp = 0; for(int i = 0; i < N; i++){ scanf("%lf%lf",&x,&y); point[i] = Point(x, y); for(int j = 0; j < i; j++){ dt[tp++] = point[i] + point[j]; } } if(N == 1){ puts("1");continue; } sort(dt, dt + tp); int ans = 0, temp = 0; for(int i = 1; i < tp; i++){ if(dt[i] == dt[i - 1]){ temp++; ans = max(ans,temp); } else temp = 0; } ans++; printf("%d\n", getn(1, -1, -2 * ans) ); } return 0; }