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=;i<n;i++)
scanf("%d%d",&pnt[i].x,&pnt[i].y);
max=;
for(i=;i<n;i++)
for(j=i+;j<n;j++)
{
cnt=;
for(k=j+;k<n;k++)
{
if(k==i||k==j) continue;
int left=(pnt[i].x-pnt[k].x)*(pnt[j].y-pnt[k].y);
int right=(pnt[j].x-pnt[k].x)*(pnt[i].y-pnt[k].y);
if(left==right) cnt++;
}
max=max>cnt? max:cnt;
}
printf("%d\n",max+);
}
return ;
}

poj 2606

#include <iostream>
#include <cstdio>
#include <memory.h>
using namespace std;
const int maxn=;
struct point
{
int x;
int y;
} pnt[maxn];
int main()
{
//freopen("in.txt","r",stdin);
int n,max,cnt;
while(cin >> n)
{
memset(pnt,,sizeof(pnt));
for(int i=;i<n;i++)
{
scanf("%d%d",&pnt[i].x,&pnt[i].y);
}
max=;
for(int i=;i<n;i++)
{
for(int j=i+;j<n;j++)
{
cnt=;
for(int k=j+;k<n;k++)
{
if(k==i || k==j)continue;
int left=(pnt[i].x-pnt[k].x)*(pnt[j].y-pnt[k].y);
int right=(pnt[j].x-pnt[k].x)*(pnt[i].y-pnt[k].y);
if(left==right)cnt++;
}
if(cnt>max)max=cnt;
}
}
cout << max+ << endl;
}
return ;
}

poj 2780

此题数据量比较大,同时虽然给的是3000ms,用n3算法也会超时,时间卡的紧,故一定要用n2lgn算法:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = ;
const double eps = 1e-;
struct point
{
int x,y;
}node[maxn];
double k[maxn];
int n;
bool equal(double x,double y)
{
if(abs(x-y) <= eps)
{
return true;
}
return false;
}
int max(int a,int b)
{
return a > b ? a : b;
}
void read()
{
for(int i=;i<n;i++)
{
scanf("%d %d",&node[i].x,&node[i].y);
}
return;
}
void solve()
{
int ans = ;
for(int i=;i<n-;i++)
{
int top = ;
int tmp = ;
for(int j=i+;j<n;j++)
{
if(node[i].x == node[j].x)
{
tmp++;
}
else
{
k[top++] = (double)(node[j].y - node[i].y) / (node[j].x - node[i].x);
}
}
sort(k,k+top);
int cnt = ;
for(int j=;j<top;j++)
{
if(j < top- && equal(k[j],k[j+]))
{
cnt++;
}
else
{
tmp = max(tmp , cnt);
cnt = ;
}
}
ans = max(ans , tmp);
}
printf("%d\n",ans+);
return;
}
int main()
{
while(~scanf("%d",&n))
{
read();
solve();
}
return ;
}

注意对于每个点,都要求出当前对于此点的无斜率情况和最大斜率的数目的max

之后枚举每个点的上述max

之前wa了无数次,承蒙九龙大神给了一个平行四边形的测试数据才豁然开朗,同时当时对与斜率不存在的情况也没有依点而分析,造成错误在所难免,下附原来的wrong answer 代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <memory.h>
using namespace std;
const int maxn=;
double s[+];
struct location_
{
int x;
int y;
}l[maxn];
double slope(int m,int n)
{
double ans=(double)(l[m].y-l[n].y)/(double)(l[m].x-l[n].x);
return ans;
}
int main()
{
//freopen("in.txt","r",stdin);
int n;
while(cin >> n)
{
memset(l,,sizeof(l));
memset(s,0.0,sizeof(s));
int k=-;
int slo_cnt=;
for(int i=;i<=n;i++)
{
scanf("%d%d",&l[i].x,&l[i].y);
}
for(int i=;i<=n-;i++)
{
for(int j=i+;j<=n;j++)
{
if((l[i].x!=l[j].x))
{
k++;
s[k]=slope(i,j);
}
else
slo_cnt++;
}
}
for(int i=;i<=;i++)
{
if(i*(i-)/==slo_cnt)
{
slo_cnt=i;
break;
}
}
// cout << slo_cnt << ">>"<< endl;
// for(int i=0;i<=k-1;i++)
// {
// cout << i << ' ' <<s[i] << endl;
// }
sort(s,s+k);
// for(int i=0;i<=k-1;i++)
// {
// cout << i << ' ' <<s[i] << endl;
// }
int cnt=;
int max=;
for(int i=;i<=k-;i++)
{
if(fabs(s[i]-s[i-])<=0.00000001)
{
cnt++;
if(max<cnt)max=cnt;
}
else cnt=;
}
max++;
// cout << "dsdsdsds"<< " " <<max << endl;
for(int i=;i<=;i++)
{
if(i*(i-)/==max)
{
max=i;
break;
}
}
// cout << max<<" max " << endl;
// cout << slo_cnt << " slo_cnt "<<endl;
if(slo_cnt>max)max=slo_cnt;
cout << max << endl;
}
return ;
}

【同一直线最多点】 poj 1118+2606+2780的更多相关文章

  1. HDU 1432 Lining Up (POJ 1118)

    枚举,枚举点 复杂度为n^3. 还能够枚举边的,n*n*log(n). POJ 1118 要推断0退出. #include<cstdio> #include<cstring> ...

  2. POJ 1118 Lining Up 直线穿过最多的点数

    http://poj.org/problem?id=1118 直接枚举O(n^3) 1500ms能过...数据太水了...这个代码就不贴了... 斜率排序O(n^2logn)是更好的做法...枚举斜率 ...

  3. 简单几何(直线求交点) POJ 2074 Line of Sight

    题目传送门 题意:从一条马路(线段)看对面的房子(线段),问连续的能看到房子全部的最长区间 分析:自己的思路WA了:先对障碍物根据坐标排序,然后在相邻的障碍物的间隔找到区间,这样还要判断是否被其他障碍 ...

  4. 简单几何(线段与直线的位置) POJ 3304 Segments

    题目传送门 题意:有若干线段,问是否存在一条直线,所有线段投影到直线上时至少有一个公共点 分析:有一个很好的解题报告:二维平面上线段与直线位置关系的判定.首先原问题可以转换为是否存在一条直线与所有线段 ...

  5. UVa 270 & POJ 1118 - Lining Up

    题目大意:给一些点,找出一条直线使尽可能多的点在这条直线上,求这条直线上点的个数. 以每一个点为原点进行枚举,求其它点的斜率,斜率相同则说明在一条直线上.对斜率排序,找出斜率连续相等的最大长度. #i ...

  6. POJ 1118 求平面上最多x点共线

    题意:给你n个点的坐标.求一条直线最多能穿过多少个点. 思路:枚举(n^2)+求斜率+排序 (复杂度n^2logn)大功告成 //By: Sirius_Ren #include <cmath&g ...

  7. poj 1118 Lining Up(水题)

    再思考一下好的方法,水过,数据太弱! 本来不想传的! #include <iostream> using namespace std; #define MAX 702 /*284K 422 ...

  8. POJ 1118 Lining Up

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

  9. POJ 1118

    #include<iostream> #include<set> #include<stdio.h> #include<math.h> #include ...

随机推荐

  1. 《JS权威指南学习总结--9.1 类和模板》

    内容要点: 一.JS类 在JS中,类的所有实例对象都从同一个原型对象上继承属性.因此,原型对象是类的核心.在例6.1 原型中定义了inherit()函数(通过原型继承创建一个新对象),这个函数返回一个 ...

  2. MongoDB数据模型(一)

    原文地址 一.数据模型介绍 MongoDB中的数据有着灵活的架构.与SQL数据库不同,因为SQL数据库必须先定义表结构,然后才能向其中插入数据,而MongoDB的集合不强制任何文档结构.这个灵活性方便 ...

  3. ResultSet 的Type属性 TYPE_FORWARD_ONLY, TYPE_SCROLL_I

    说明:Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY ...

  4. JSON.parse()和JSON.stringify()&&traditional(ajax请求)的作用

    parse是一个字符串中解析出json对象,如 var str = '{"name":"haizeiwang"}' 结果: JSON.parse(str) na ...

  5. C#第九天

    1.绝对路径和相对路径绝对路径:通过给定的这个路径直接能在我的电脑中找到这个文件. 相对路径:文件相对于应用程序的路径. 结论:我们在开发中应该去尽量的使用相对路径. 2.装箱.拆箱 装箱:就是将值类 ...

  6. AIR文件操作:使用文件对象操作文件和目录 .

    来源:http://blog.csdn.net/zdingxin/article/details/6635376 在AIR中可以方便的对本地文件操作,不过上次做了个项目,发现还是有不少不方便的地方,比 ...

  7. Mybatis批量更新数据

    转载:http://blog.csdn.net/tolcf/article/details/39213217 第一种方式 <update id="updateBatch" p ...

  8. XCode工程内多Targets

    XCode工程内多Targets 可以认为一个target对应一个新的product(基于同一份代码的情况下). 虽然代码是同一份, 但编译设置(比如编译条件), 以及包含的资源文件却可以有很大的差别 ...

  9. RSA 公私钥 互换问题

    关于 RSA,我的理解是: 按定义的话,公私钥是可以互换的 问题是常见的实现里面,保存“私钥”的那个数据结构或者文件,里面除了私钥所必需的那一对数之外,还有额外的信息(足以算出公钥来),所以绝对不能把 ...

  10. 在 Linux 环境下报错 java.lang.reflect.InvocationTargetException

    今天开发了一个 excel 导出数据的功能,放到 linux 服务器上后发现报错. 捕获到 java.lang.reflect.InvocationTargetException 异常,这个异常不太常 ...