Lining Up(在一条直线上的最大点数目,暴力)

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;
}
时间: 2024-10-06 10:58:42

Lining Up(在一条直线上的最大点数目,暴力)的相关文章

UVA Lining Up (一条直线上最多的点数)

Description  Lining Up  ``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

一条直线上N个线段所覆盖的总长度

原文:http://blog.csdn.net/bxyill/article/details/8962832 问题描述: 现有一直线,从原点到无穷大. 这条直线上有N个线段.线段可能相交. 问,N个线段总共覆盖了多长?(重复覆盖的地区只计算一次) ================================================ 解题思路: 可以将每个线段拆分成“单位1” 遍历所有线段,使用一个数组记录每个线段所走过的“单位1” 最后统计数组中被走过的中“单位1”的个数,即是所有线

149. Max Points on a Line *HARD* 求点集中在一条直线上的最多点数

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. /** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(int a, int b) : x(a), y(b) {} * }; */ class Solutio

[LintCode] 最多有多少个点在一条直线上

1 /** 2 * Definition for a point. 3 * struct Point { 4 * int x; 5 * int y; 6 * Point() : x(0), y(0) {} 7 * Point(int a, int b) : x(a), y(b) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 /** 13 * @param points an array of point 14 * @return an inte

objectarx之判断三点是否在一条直线上

bool CCommonFuntion::IsOnLine(AcGePoint2d& pt1, AcGePoint2d& pt2, AcGePoint2d& pt3){ AcGeVector3d vec1 = AcGeVector3d(pt2.x - pt1.x, pt2.y - pt1.y, 0); AcGeVector3d vec2 = AcGeVector3d(pt3.x - pt1.x, pt3.y - pt1.y, 0); double pi = 3.14159265;/

判断两条直线的位置关系 POJ 1269 Intersecting Lines

两条直线可能有三种关系:1.共线     2.平行(不包括共线)    3.相交. 那给定两条直线怎么判断他们的位置关系呢.还是用到向量的叉积 例题:POJ 1269 题意:这道题是给定四个点p1, p2, p3, p4,直线L1,L2分别穿过前两个和后两个点.来判断直线L1和L2的关系 这三种关系一个一个来看: 1. 共线. 如果两条直线共线的话,那么另外一条直线上的点一定在这一条直线上.所以p3在p1p2上,所以用get_direction(p1, p2, p3)来判断p3相对于p1p2的关

149 Max Points on a Line 直线上最多的点数

给定二维平面上有 n 个点,求最多有多少点在同一条直线上. 详见:https://leetcode.com/problems/max-points-on-a-line/description/ /** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(int a, int b) : x(a), y(b) {} * }; */ class Solution {

Max Points on a Line(直线上最多的点数)

给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | |        o |     o |  o   +-------------> 0  1  2  3 4 示例 2: 输入: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]] 输出: 4 解释: ^ | | o |     o   o |      o |  o   o +------------------

[Swift]LeetCode149. 直线上最多的点数 | Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. Example 1: Input: [[1,1],[2,2],[3,3]] Output: 3 Explanation: ^ | |        o |     o |  o   +-------------> 0  1  2  3 4 Example 2: Input: [[1,1],[3,2]