zoukankan      html  css  js  c++  java
  • 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
    ", getn(1, -1, -2 * ans) );
        }
        return 0;
    }

     java:

    package com.lanqiao.week1;
    
    import java.util.Arrays;
    import java.util.Scanner;
    
    public class poj1118 {
        private static Scanner cin;
        private static int MOD = 1000000007;
        static{
            cin = new Scanner(System.in);
        }
        static int getN(double a, double b, double c){
            double ans = (-b + Math.sqrt(b * b - 4 * a * c)) / (2.0 * a);
            return (int)ans;
        }
        static class Point{
            int x, y;
            public static Node getNode(Point a, Point b) {
                int x = a.x - b.x;
                int y = a.y - b.y;
                double k = 1.0*y/x;
                return new Node(k, a.y - a.x * k);
            }
        }
        static class Node implements Comparable<Node>{
            double k, t;
    
            public Node(double k, double t) {
                super();
                this.k = k;
                this.t = t;
            }
    
            public static boolean isEqual(Node a, Node b){
                if(Math.abs(a.k - b.k) <= 1e-15 && 
                        Math.abs(a.t - b.t) <= 1e-15){
                    return true;
                }else
                    return false;
            }
            @Override
            public int compareTo(Node o) {
                if(Math.abs(o.k - k) <= 1e-15){
                    if(o.t < t){
                        return 1;
                    }else{
                        return -1;
                    }
                }else{
                    if(o.k < k){
                        return 1;
                    }else{
                        return -1;
                    }
                }
            }
            
            
        }
        static Point[] points = new Point[710];
        static Node[] nodes = new Node[250000];
        public static void main(String[] args) {
            int N;
            N = cin.nextInt();
            while(N > 0){
                
                int k = 0;
                for(int i = 0; i < N; i++){
                    points[i] = new Point();
                    points[i].x = cin.nextInt();
                    points[i].y = cin.nextInt();
                    for(int j = 0; j < i; j++){
                        nodes[k++] = Point.getNode(points[i], points[j]);
                    }
                }
                Arrays.sort(nodes, 0, k);
    //            for(int i = 0; i < k; i++){
    //                System.out.println((i + 1) + " : " + "k-->" + nodes[i].k + "t-->" + nodes[i].t);
    //            }
                int ans = 1, cnt = 1;
                for(int i = 1; i < k; i++){
                    if(Node.isEqual(nodes[i], nodes[i - 1])){
                        cnt ++;
                        ans = Math.max(ans, cnt);
                    }else{
                        cnt = 1;
                    }
                }
                System.out.println(getN(1, -1, -2*ans));
                N = cin.nextInt();
            }
        }
    }
  • 相关阅读:
    Masscan入门手册
    Deepin安装Python3
    docker 配置 kafka+zookeeper,golang操作kafka
    VMware安装Centos7超详细过程(图文)
    国家代码查询
    thinkphp5 关于跨域的一些坑,附上解决办法(比较全面了)
    网络协议分析与抓包 TCP/IP UDP等
    一维数组分组成二维数组
    Fiddler在抓取https数据包时如何解决Tunnel to 443的问题?
    十条nmap常用的扫描命令
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5356131.html
Copyright © 2011-2022 走看看