POJ 1118 Lining Up

枚举,排序。

先将所有点按双关键字排序,然后枚举线的顶点$P$,剩余的点以$P$为中心进行极角排序,可以取个$gcd$,这样一样的点就排在一起了,然后统计一下更新答案。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-8;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c = getchar(); x = 0;while(!isdigit(c)) c = getchar();
    while(isdigit(c)) { x = x * 10 + c - ‘0‘; c = getchar();  }
}

const int maxn=800;
int n,ans;
struct X{LL x,y;}s[maxn],t[maxn];

LL gcd(LL a,LL b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}

bool cmp(X a,X b)
{
    if(a.x==b.x) return a.y<b.y;
    return a.x<b.x;
}

LL ABS(LL a)
{
    if(a<0) a=-a;
    return a;
}

int main()
{
    while(~scanf("%d",&n))
    {
        if(n==0) break; ans=1;
        for(int i=1;i<=n;i++) scanf("%lld%lld",&s[i].x,&s[i].y);
        sort(s+1,s+1+n,cmp);
        for(int i=1;i<=n;i++)
        {
            int sz=0;
            for(int j=i+1;j<=n;j++)
            {
                t[sz].x=s[j].x-s[i].x;
                t[sz].y=s[j].y-s[i].y;
                LL GCD=gcd(ABS(t[sz].x),ABS(t[sz].y));
                t[sz].x=t[sz].x/GCD;
                t[sz].y=t[sz].y/GCD;
                sz++;
            }

            if(sz==0) continue;
            sort(t,t+sz,cmp);

            int k=1;
            for(int i=1;i<sz;i++)
            {
                if(t[i].x==t[i-1].x&&t[i].y==t[i-1].y) k++;
                else  ans=max(ans,k+1),k=1;
            }
            ans=max(ans,k+1);
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-10 07:27:31

POJ 1118 Lining Up的相关文章

HDU 1432 Lining Up (POJ 1118)

枚举,枚举点 复杂度为n^3. 还可以枚举边的,n*n*log(n). POJ 1118 要判断0退出. #include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<map> #include<stack> #include<iostream> #include<list&

POJ 1118 最多共线点

Lining Up Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 22516   Accepted: 7091 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 spe

【同一直线最多点】 poj 1118+2606+2780

poj 1118 #include<iostream> using namespace std; #define N 700 struct point {int x,y;} pnt[N]; int main() { int n,i,j,k,max,cnt; while(scanf("%d",&n)&&n) { for(i=0;i<n;i++) scanf("%d%d",&pnt[i].x,&pnt[i].y)

Lining Up Rabbit hunt poj 1118 poj 2606 点共线问题。

思路:首先把所有点按照坐标排序,然后以每一个点为基准点,计算其他点与这个点连线的斜率,将所有斜率排序后求最多的相同的斜率数即可. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string> 7 #include <vector> 8 #

1118 Lining Up

题目链接: http://poj.org/problem?id=1118 题意: 给定n个点, 求在同一直线上的点最多的直线上点的数目. 解法: 简单题目, 规模比较小,  暴力搜索. 1 #include <iostream> 2 using namespace std; 3 #define MAXN 700 4 struct Point{ 5 int x,y; 6 }p[MAXN]; 7 int main(){ 8 int N; 9 cin>>N; 10 while(N){ 1

POJ 1118与2606 (水题)

[题意简述]:两道题都是求有多少点在一条直线上. [分析]:暴力然后使用斜率相等进行求解,但要注意的是在求斜率时,避免使用除法,一律换位乘法运算,否则会RE. 以2606为例贴代码,1118只是数组的大小不一样,其他相同. //216K 0Ms #include<iostream> using namespace std; int a[205],b[205]; int main() { int t; cin>>t; int res ; int ans = 0; for(int i

POJ 1118

#include<iostream> #include<set> #include<stdio.h> #include<math.h> #include<algorithm> #define MAXN 705 using namespace std; int num; double p[MAXN][2]; double a[MAXN*MAXN]; set<int> coll; set<int>::iterator pos;

优质题表(机密版)

转载请注明出处:http://www.cnblogs.com/dashuzhilin/p/4556803.html 思维题: poj 1528 poj 1597 poj 2538 poj 2608 poj 2612 poj 2361 poj 2339 poj 2664 uva 10894 uva 10921   uva 10922   uva 10929 uva 10931   uva 10800   uva 10878 uva 10976   uva 10323   uva 201 poj 2

POJ 1018 &amp; HDU 1432 Lining Up 【简单几何】

Lining Up Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 24786   Accepted: 7767 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 spe