天天看点

POJ 1118 Lining Up

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

Input consist several case,First line of the each case is an integer N ( 1 < N < 700 ),then follow N pairs of integers. Each pair of integers is separated by one blank and ended by a new-line character. The input ended by N=0.

Output

output one integer for each input case ,representing the largest number of points that all lie on one line.

Sample Input

5
1 1
2 2
3 3
9 10
10 11
0      

Sample Output

3

统计最多有多少点共线,直接枚举起始点,把剩余点按斜率排序统计相同的个数即可。

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define fi first
#define se second
#define mp(i,j) make_pair(i,j)
#define pii pair<string,string>
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-6;
const int INF = 0x7FFFFFFF;
const int mod = 9973;
const int N = 5e3 + 10;
int T, n, x[N], y[N];
double g[N];

int main()
{
  while (scanf("%d", &n), n)
  {
    rep(i, 1, n) scanf("%d%d", &x[i], &y[i]);
    int ans = 0;
    rep(i, 1, n)
    {
      int h = 1, k = 0, c = 1;
      rep(j, i + 1, n)
      {
        if (x[i] == x[j]) { h++; continue; }
        g[k++] = 1.0*(y[j] - y[i]) / (x[j] - x[i]);
      }
      sort(g, g + k);
      ans = max(ans, h);
      rep(j, 1, k)
      {
        if (j < k && fabs(g[j] - g[j - 1]) < eps) c++;
        else ans = max(ans, c + 1), c = 1;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}