hdoj 1432 Lining Up 【思维】

题目链接:hdoj 1432 Lining Up

Lining Up

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1255 Accepted Submission(s): 359

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

题意:有n个点,问你共线的点最多有多少个。

思路:我们处理出斜率,统计斜率的个数就可以了。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2*1e5 + 10;
const int pN = 1e6;// <= 10^7
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
int x[701], y[701];
double k[700*700];
int main() {
    int n; 
    while(scanf("%d", &n) != EOF) {
        for(int i = 0; i < n; i++) {
            scanf("%d%d", &x[i], &y[i]);
        }
        int ans = 0;
        for(int i = 0; i < n; i++) {
            int top = 0;
            for(int j = i+1; j < n; j++) {
                k[top++] = (y[j] - y[i]) * 1.0 / (x[j] - x[i]);
                //cout << k[top-1] << endl;
            }
            sort(k, k+top); int cnt = 1; int ans1 = 2;
            for(int j = 1; j < top; j++) {
                if(k[j] == k[j-1]) cnt++;
                else {
                    ans1 = max(ans1, cnt+1);
                    cnt = 1;
                }
            }
            ans1 = max(ans1, cnt+1);
            ans = max(ans, ans1);
        }
        printf("%d\n", ans);
    }
    return 0;
}

你可能感兴趣的:(hdoj 1432 Lining Up 【思维】)