UVa 270 / POJ 1118 Lining Up:计算几何

270 - Lining Up

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=206

http://poj.org/problem?id=1118

``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

思路:

任取一点,计算出其他点到该点斜率后排序,斜率相同的点必然在一条直线上。 复杂度:O(N^2*log N)

UVa的代码:

/*0.129s*/

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF = 1 << 30;

char str[20];
double x[705], y[705], d[705];

int main(void)
{
    int t, n, i, j, k;
    int ans, count;
    scanf("%d\n", &t);///多读一个换行~
    while (t--)
    {
        for (n = 0; gets(str); ++n)
        {
            if (str[0] == '\0') break;
            sscanf(str, "%lf%lf", &x[n], &y[n]);
        }
        //////
        ans = 1;
        for (i = 0; i < n - 1; ++i)
        {
            for (j = i + 1, k = 0; j < n; ++j, ++k)
                d[k] = (x[j] == x[i] ? INF : (y[j] - y[i]) / (x[j] - x[i]));
            sort(d, d + k);
            count = 1;
            for (j = 1; j < k; ++j)
            {
                if (fabs(d[j] - d[j - 1]) < 1e-9)
                {
                    ++count;
                    ans = max(ans, count);
                }
                else count = 1;
            }
        }
        printf("%d\n", ans + 1);
        if (t) putchar('\n');
    }
    return 0;
}

查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

POJ的代码:

/*125ms,204KB*/

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF = 1 << 30;

char str[20];
double x[705], y[705], d[705];

int main(void)
{
    int n, i, j, k;
    int ans, count;
    while (scanf("%d", &n), n)
    {
        for (i = 0; i < n; ++i)
            scanf("%lf%lf", &x[i], &y[i]);
        ans = 1;
        for (i = 0; i < n - 1; ++i)
        {
            for (j = i + 1, k = 0; j < n; ++j, ++k)
                d[k] = (x[j] == x[i] ? INF : (y[j] - y[i]) / (x[j] - x[i]));
            sort(d, d + k);
            count = 1;
            for (j = 1; j < k; ++j)
            {
                if (fabs(d[j] - d[j - 1]) < 1e-9)
                {
                    ++count;
                    ans = max(ans, count);
                }
                else count = 1;
            }
        }
        printf("%d\n", ans + 1);
    }
    return 0;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索number
, line
, of
, The
blank
poj1118、lining、silver lining、streamlining、lining 棉衣,以便于您获取更多的相关知识。

时间: 2024-11-03 04:22:04

UVa 270 / POJ 1118 Lining Up:计算几何的相关文章

poj 1118 Lining Up【同一条直线上的点】

这道题总算没有让我感觉超级水,至少我还超时了一次...哈哈哈 题意还比较容易懂:给出 n 个点的整数坐标(n<=700),求一条直线,使得在这条直线上的点数最多,输出点数. 解题思路:采用几何中的三个点是否在一条直线上判定定理:(yi-yk)/(xi-xk)=(yj-yk)/(xj-xk),除法不能出现分母为0的情况,所以转换为乘法做(而且乘法效率也高些),即:(y[i]-y[k])*(x[j]-x[k])==(y[j]-y[k])*(x[i]-x[k])(i.j.k共线). 暴搜肯定尽量追求不

UVa 270:Lining Up

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=206 原题: ``How am I ever going to solve this problem?" said the pilot. Indeed, the pilot was not facing an easy task. She h

POJ 3218 TOYS(计算几何)(二分)

TOYS:http://poj.org/problem?id=2318 大意:给你一个箱子,有n个挡板分隔成n+1部分,给你m个玩具的坐标,问每一部分有几个玩具. 思路:举对每个玩具,二分线段下标,判断玩具在线段左边还是右边,枚举后统计. 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/ #include <map> #include <stack> #include <queue>

uva 270 - Lining Up

点击打开链接uva270 题目意思:    给定平面上的n个点,要求在同一条直线上最多几个 解题思路:    枚举所有解                      1:三个点共线的性质:A(X1,Y1),B(X2,Y2),C(X3,Y3);这个时候有(Y2-Y1)/(X2-X1) = (Y3-Y1)/(X3-X1),我们知道对于double类型是不能够直接进行比较的,所以由这个式子可以变形得到:(Y2-Y1)*(X3-X1) = (Y3-Y1)*(X2-X1).                

POJ 1265 Area (计算几何)(Pick定理)

Area:http://poj.org/problem?id=1265 计算几何)(Pick定理)-poj1265"> 大意:每次给你一个点的横纵坐标变化值,求有多少点在多边形上,有多少点在多边形内,和多边形的面积. 思路:Pick定理. 一个计算点阵中顶点在格点上的多边形面积公式:S=a+b÷2-1,其中a表示多边形内部的点数,b表示多边形边界上的点数,s表示多边形的面积. 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Progr

UVa 748/POJ 1001 Exponentiation:浮点高精度求幂&amp;amp;正则表达式的应用

748 - Exponentiation Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=97&page=show_problem&problem=689 http://poj.org/problem?id=1001 Problems involving the computation of exact values

UVa 755 / POJ 1002 487--3279 (排序)

755 - 487--3279 Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem&problem=696 http://poj.org/problem?id=1002 Businesses like to have memorable telephone numbers. On

UVa 113 / POJ 2109 Power of Cryptography

使用double处理大整数&泰勒公式与误差分析 113 - Power of Cryptography Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=49 http://poj.org/problem?id=2109 Background Curre

UVa 10038 / POJ 2575 / ZOJ 1879 Jolly Jumpers (water ver.)

10038 - Jolly Jumpers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=979 http://poj.org/problem?id=2575 http://acm.zju.edu.cn/onlinejudge/showProblem.do?