UVa 270/poj 1118 lining up: Computational geometry

Source: Internet
Author: User
Tags printf sort time limit cmath

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 I ever going to solve this problem? ' said the pilot.

Indeed, the pilot is not facing a 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 Pos Sible. All points were given by means of the integer coordinates in a two-dimensional spaces. The pilot wanted to know the largest number of points from the given set, all lie on one line. Can you write a program so calculates this number?

Your program has to be efficient!

Input

The input begins with a single positive integer in a line by itself indicating the number of cases following, each of them as described below. This are followed by a blank line, and there are also a blank line between two consecutive. The input consists of N pairs of integers, where 1 < N < 700. Each pair of integers was 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 would occur twice.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases is separated by a blank line. The output consists of one integer representing the largest number of points that all lie to one line.

Sample Input

1

1 1
2 2 3 3 9
ten
10 11

Sample Output

3

Ideas:

At any point, calculate the other points to the point of the slope after the order, the slope of the same points must be in a straight line. Complexity: O (N^2*log N)

UVA Code:

/*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);///read a newline ~ while (t--) {for (n = 0; gets (str); ++n) {if (str[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; }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Code for POJ:

/*125ms,204kb*/
  
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace Std;
const int INF = 1 <<;
  
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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.