UVA 270-Lining Up(多点共线)

Lining Up

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld
& %llu

Submit Status

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 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 begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank
line between two consecutive inputs.

The input consists of N pairs of integers, where 1 < N < 700. Each pair of integers is separated by one blank and ended by a new-line character. The list of pairs is ended with an end-of-file character. No pair will occur twice.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

The output consists of one integer representing the largest number of points that all lie on one line.

Sample Input

1

1 1
2 2
3 3
9 10
10 11

Sample Output

3

题意:问在给出的点中,最多有多少点在一条直线上。

Ps:一定要注意输入,真是坑哭了。UVA的格式错误返回的是WA saddd

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
using namespace std;
const int inf=0x3f3f3f3f;
struct node
{
    int x,y;
}q[1010];
char str[1010];
int main()
{
    int T,cnt,i,j,k;
    int sum,res;
    scanf("%d",&T);
    getchar();
    gets(str);
    while(T--){
        cnt=0;
        res=-inf;
        while(gets(str)){
            if(str[0]=='\0') break;
            sscanf(str,"%d %d",&q[cnt].x,&q[cnt].y);
            cnt++;
        }
        for(i=0;i<cnt;i++)
        for(j=i+1;j<cnt;j++){
            sum=2;
            for(k=j+1;k<cnt;k++)
                if((q[j].y-q[i].y)*(q[k].x-q[j].x)==(q[j].x-q[i].x)*(q[k].y-q[j].y))
                sum++;
            res=max(res,sum);
        }
        printf("%d\n",res);
        if(T)
            printf("\n");
    }
    return 0;
}
时间: 2024-08-09 09:41:12

UVA 270-Lining Up(多点共线)的相关文章

uva 270 Lining Up (几何)

uva 270 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 the a

UVa 270 - Lining Up

题目:给你平面上n个点,求最多有几个点共线. 分析:计算几何.枚举基准点,其他点按基准点极角排序(斜率排序),然后枚举相邻直线判断统计即可. 说明:时间复杂度O(n*n*lg(n)). #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> using namesp

uva 270 Lining Up(暴力)

这道题目我是暴力做出来的,复杂度是n^3,因为数组做多有700组,大约可以用这个复杂度,虽然严格来说500多才 是正常的,每次都是选择两个坐标然后确定一条直线,然后遍历一下其他点,用叉积形式判一下是否在一条直线上就ok 啦,网上说可以用极角排序来解,复杂度是n^2logn然而我看了看并没有想学的欲望...以后再学吧,,,还用到了 sscanf函数,看别人这样用的,就是在一个数组里匹配想要的类型...这题比较坑的就是输入了.每两个输出之间 有空行,最后一组后面不能加空行...而且坐标输入之前有空行

poj2780Linearity(多点共线)

链接 判断最多多少点在一条直线上, 可以枚举每一个点为坐标系的原点,其它点变成相应的位置,然后求得过原点及其点的斜率,排序找一下最多相同的. 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8

关于多点共线问题

想了想这个问题,自己写了写,之前百度了个答案,思想基本类似 #include <vector>#include <map> class point { public:     point(int x0,int x1):x(x0),y(x1){};     ~point(){};     int getX() const {return x;}     int getY() const {return y;} private:     int x;     int y; }; int

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

HDOJ 4463 Outlets 最小生成树

Prim....似乎没有考虑多点共线也能A..... Outlets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2565    Accepted Submission(s): 1182 Problem Description In China, foreign brand commodities are often much mo

poj1584 A Round Peg in a Ground Hole 判断多边形凹凸,点到线的距离【基础计算几何】

大致思路:首先对于所给的洞的点,判断是否是凸多边形,图形的输入和输出可以是顺时针或者逆时针,而且允许多点共线 Debug 了好几个小时,发现如下问题 判断三点是否共线,可用斜率公式判断 POINT point_A, point_B, point_C; if(point_A.x == point_B.x || point_B.x == point_C.x){ if(point_A.x == point_B.x && point_B.x == point_C.x) continue; }els

atcoder #082 E 暴力 计算几何

给出点集,然后求一个凸包的所有的子凸包的贡献总和,贡献计算是凸包内部含边界上点的数量N,凸包的不包含边界的顶点数S,贡献为$2^{N-S}$ 首先很容易想到,凸包上包含内部的所有点构成的子凸包有Sum(i = 3 ->N)C(i,N)种情况,这个式子其实就是二项式的一部分.但是有可能出现多点共线的不合法情况,所以问题转换为求所有点构成的直线中,每条直线上大于2点的点的数目,每条直线都要分别计算,最后减去就行了.求共线可以用叉积可以用斜率,注意判重. 这场比赛迟了10分钟才写,这题开始还在用凸包搞