【同一直线最多点】 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=;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的更多相关文章
- HDU 1432 Lining Up (POJ 1118)
枚举,枚举点 复杂度为n^3. 还能够枚举边的,n*n*log(n). POJ 1118 要推断0退出. #include<cstdio> #include<cstring> ...
- POJ 1118 Lining Up 直线穿过最多的点数
http://poj.org/problem?id=1118 直接枚举O(n^3) 1500ms能过...数据太水了...这个代码就不贴了... 斜率排序O(n^2logn)是更好的做法...枚举斜率 ...
- 简单几何(直线求交点) POJ 2074 Line of Sight
题目传送门 题意:从一条马路(线段)看对面的房子(线段),问连续的能看到房子全部的最长区间 分析:自己的思路WA了:先对障碍物根据坐标排序,然后在相邻的障碍物的间隔找到区间,这样还要判断是否被其他障碍 ...
- 简单几何(线段与直线的位置) POJ 3304 Segments
题目传送门 题意:有若干线段,问是否存在一条直线,所有线段投影到直线上时至少有一个公共点 分析:有一个很好的解题报告:二维平面上线段与直线位置关系的判定.首先原问题可以转换为是否存在一条直线与所有线段 ...
- UVa 270 & POJ 1118 - Lining Up
题目大意:给一些点,找出一条直线使尽可能多的点在这条直线上,求这条直线上点的个数. 以每一个点为原点进行枚举,求其它点的斜率,斜率相同则说明在一条直线上.对斜率排序,找出斜率连续相等的最大长度. #i ...
- POJ 1118 求平面上最多x点共线
题意:给你n个点的坐标.求一条直线最多能穿过多少个点. 思路:枚举(n^2)+求斜率+排序 (复杂度n^2logn)大功告成 //By: Sirius_Ren #include <cmath&g ...
- poj 1118 Lining Up(水题)
再思考一下好的方法,水过,数据太弱! 本来不想传的! #include <iostream> using namespace std; #define MAX 702 /*284K 422 ...
- POJ 1118 Lining Up
枚举,排序. 先将所有点按双关键字排序,然后枚举线的顶点$P$,剩余的点以$P$为中心进行极角排序,可以取个$gcd$,这样一样的点就排在一起了,然后统计一下更新答案. #pragma comment ...
- POJ 1118
#include<iostream> #include<set> #include<stdio.h> #include<math.h> #include ...
随机推荐
- 图的两种遍历:DFS&BFS
DFS和BFS在图中的应用: 图连通性判定:路径的存在性:图中是否存在环:求图的最小生成树:求图的关键路径:求图的拓扑排序. DFS:简单的说,先一直往深处走,直到不能再深了,再从另一条路开始往深处走 ...
- Java 反射 Class类
Java 反射 Class类 @author ixenos 摘要:Class类在反射中的地位.构造Class对象的三种方式.Class对象构造对应类型对象的三种方式 Class类在反射中的地位 位于j ...
- 1.MyBaits无代理全套增删改
一.mybatis使用的准备工作 1.找到mybatis所需要的jar文件: mybatis-3.2.3.jar mybatis-spring-1.2.1.jar 2.解压mybatis-3.2.3. ...
- Python 邮件发送
python发送各类邮件的主要方法 python中email模块使得处理邮件变得比较简单,今天着重学习了一下发送邮件的具体做法,这里写写自己的的心得,也请高手给些指点. 一.相关模块介绍 ...
- javascript动画效果之匀速运动(修订版)
在编写多块同时触发运动的时候,发现一个BUG, timer = setInterval(show, 30);本来show是一个自定义函数,当设为timer = setInterval(show(one ...
- linux内核驱动——从helloworld开始
学习编程第一个都是学习hello world程序,学习内核驱动自然也不例外,我也是!本文整理了网上的一些资料以及加上自己的一些心得体会,希望对初学者有帮助,可别小看这个简单的hello world,本 ...
- 转载 deep learning:八(SparseCoding稀疏编码)
转载 http://blog.sina.com.cn/s/blog_4a1853330102v0mr.html Sparse coding: 本节将简单介绍下sparse coding(稀疏编码),因 ...
- Centos6.6升级python版本
centos原生python为2.6.6,可以通过下面的命令查看 #python -V Python 注:在安装新版本前,请先安装zlib\openssl组件,如果你确认你用不到这个,也可以不装 需要 ...
- 《JavaScript高级程序设计》读书笔记 ---操作符二
关系操作符 小于(<).大于(>).小于等于(<=)和大于等于(>=)这几个关系操作符用于对两个值进行比较,比较的规则与我们在数学课上所学的一样.这几个操作符都返回一个布尔值, ...
- webstoem自动编译less文件
去node的主页下载对应版本的nodejs然后安装下载地址:http://nodejs.org/ 根据自己的系统选择合适的版本下载. 安装完成之后打开命令提示符(win+r 输入cmd 回车),分 ...