zoukankan      html  css  js  c++  java
  • POJ 1118 Lining Up 直线穿过最多的点数

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

    直接枚举O(n^3) 1500ms能过...数据太水了...这个代码就不贴了...

    斜率排序O(n^2logn)是更好的做法...枚举斜率...直线方程相同的线段数量k...一定满足方程 n(n-1)/2=k  ---------------  还真是baka. 到2点才想出这个结论

    特判只有一个点,两个点的情况...500ms AC

    /********************* Template ************************/
    #include <set>
    #include <map>
    #include <list>
    #include <cmath>
    #include <ctime>
    #include <deque>
    #include <queue>
    #include <stack>
    #include <bitset>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <cassert>
    #include <cstdlib>
    #include <cstring>
    #include <sstream>
    #include <fstream>
    #include <numeric>
    #include <iomanip>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    using namespace std;
    
    #define EPS         1e-8
    #define MAXN        (int)5e5+5
    #define MOD         (int)1e9+7
    #define PI          acos(-1.0)
    #define LINF        ((1LL)<<50)
    #define INF         (1<<30)
    #define DINF        (1e10)
    #define max(a,b)    ((a) > (b) ? (a) : (b))
    #define min(a,b)    ((a) < (b) ? (a) : (b))
    #define max3(a,b,c) (max(max(a,b),c))
    #define min3(a,b,c) (min(min(a,b),c))
    #define BUG         cout<<"BUG! "<<endl
    #define LLL         cout<<"--------------"<<endl
    #define L(t)        (t << 1)
    #define R(t)        (t << 1 | 1)
    #define Mid(a,b)    ((a + b) >> 1)
    #define lowbit(a)   (a & -a)
    #define FIN         freopen("in.txt","r",stdin)
    #define FOUT        freopen("out.txt","w",stdout)
    #pragma comment     (linker,"/STACK:102400000,102400000")
    
    // typedef long long LL;
    // typedef unsigned long long ULL;
    // typedef __int64 LL;
    // typedef unisigned __int64 ULL;
    // int gcd(int a,int b){ return b?gcd(b,a%b):a; }
    // int lcm(int a,int b){ return a*b/gcd(a,b); }
    
    /*********************   F   ************************/
    struct POINT{
        double x,y;
        POINT(double _x = 0, double _y = 0):x(_x),y(_y){};
        int id;
    }p[800];
    struct LINE{
        POINT a,b;
        double K,B;
        LINE(POINT _a = 0,POINT _b = 0):a(_a),b(_b){
            if((a.x - b.x) == 0){
                K = DINF;
                B = a.x;
            }else{
                K = ((a.y - b.y) / (a.x - b.x));
                B = (- (b.x * a.y - a.x * b.y) / (a.x - b.x));
            }
        }
    }L[500000];
    bool cmp(LINE a,LINE b){
        if(a.K == b.K) return a.B < b.B;
        return a.K < b.K;
    }
    int main()
    {
        //FIN;
        int n;
        while(cin>>n && n){
            for(int i = 0 ; i < n ; i++)
                scanf("%lf%lf",&p[i].x,&p[i].y);
            if(n == 1 || n == 2){
                printf("%d
    ",n);
                continue;
            }
            int cnt = 0;
            for(int i = 0 ; i < n ; i++)
                for(int j = i+1 ; j < n ; j++)
                    L[cnt++] = LINE(p[i],p[j]);
            sort(L,L+cnt,cmp);
            int m = -INF;
            for(int i = 0 ; i < cnt-1 ; i++){
                int c = 0;
                while(L[i].K == L[i+1].K && L[i].B == L[i+1].B){
                    c++;
                    i++;
                }
                c = sqrt((c+1)*2)+1;
                m = max(m,c);
            }
            printf("%d
    ",m);
        }
        return 0;
    }
  • 相关阅读:
    Poj 2017 Speed Limit(水题)
    Poj 1316 Self Numbers(水题)
    Poj 1017 Packets(贪心策略)
    Poj 1017 Packets(贪心策略)
    Poj 2662,2909 Goldbach's Conjecture (素数判定)
    Poj 2662,2909 Goldbach's Conjecture (素数判定)
    poj 2388 Who's in the Middle(快速排序求中位数)
    poj 2388 Who's in the Middle(快速排序求中位数)
    poj 2000 Gold Coins(水题)
    poj 2000 Gold Coins(水题)
  • 原文地址:https://www.cnblogs.com/Felix-F/p/3244878.html
Copyright © 2011-2022 走看看